1

I'm having a hard time figuring out why GCC 4.5 won't let me compile this:

#include <iostream>
#include <bitset>

#define WIDTH 512
#define HEIGHT 512

#define CEIL_POS(X) ((X - (unsigned int)(X)) > 0 ? (unsigned int)(X + 1) : (unsigned int)(X))

int main ()
{
    const unsigned int length = static_cast<const unsigned int>(CEIL_POS(static_cast<float>(WIDTH * HEIGHT) / 8.0));

    std::bitset<length> bits;

    return 0;
}

It works just fine in VS2010. What am I missing?

UPDATE: I was in a hurry and I didn't paste the entire code. Sorry about that :(

PS: Just as the title says, the error that I receive is: "length cannot appear in a constant-expression."

Mihai Todor
  • 8,014
  • 9
  • 49
  • 86

1 Answers1

1

I don't know whether the problem you're having is caused by a bug in the compiler, or if that is expected behavior, but simply removing the static_cast to float seems to solve the problem, and results in the exact same value.

#include <iostream>
#include <bitset>

#define WIDTH 512
#define HEIGHT 512

#define CEIL_POS(X) ((X - (unsigned int)(X)) > 0 ? (unsigned int)(X + 1) : (unsigned int)(X))

int main ()
{
    const unsigned int length_1 = static_cast<const unsigned int>(CEIL_POS(static_cast<float>(WIDTH * HEIGHT) / 8.0));
    const unsigned int length_2 = static_cast<const unsigned int>(CEIL_POS(WIDTH * HEIGHT / 8.0));

    std::cout << length_1 << '\n' << length_2 << '\n';
    if (length_1 == length_2)
        std::cout << "They are exactly the same.";

    std::bitset<length_2> bits;
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • @stefan: What options are you using? I'm on 4.6.1 with `-Wall -Werror -pedantic-errors` and it compiles fine. – Benjamin Lindley Aug 19 '12 at 22:57
  • @BenjaminLindley For some reason, I was under the impression that the outcome of the division would be an int if the first operand is an int. Now it's working just fine. Thanks a million! – Mihai Todor Aug 19 '12 at 22:57
  • I just copy pasted this to have a double check. I'm using the exact same code and the exact same flags. It's 4.6.3 on Ubuntu 32bit. This is very strange but luckily it solves the problem of the OP.. – stefan Aug 19 '12 at 23:01
  • @stefan I'm compiling on x64. Maybe the x86 version is even more pretentious for some reason... – Mihai Todor Aug 19 '12 at 23:04
  • @MihaiTodor: Nope, I'm on 32-bit Ubuntu as well. – Benjamin Lindley Aug 19 '12 at 23:06
  • @BenjaminLindley in my case, it only works without the `-pedantic` flag. It worked in a small test, where I wasn't using any flags. I guess I can live without `-pedantic`, but this is a really nasty bug and it's even complaining if I remove the `CEIL_POS` call. It only works if I assign a literal value to `length` when compiling with `-pedantic` – Mihai Todor Aug 19 '12 at 23:35
  • 1
    I've started a new question http://stackoverflow.com/questions/12030885/static-cast-float-bitset-const-weirdness with a reduced example and adressing the compiler flags – stefan Aug 19 '12 at 23:47