1

GCC4.5でこれをコンパイルできない理由を理解するのに苦労しています。

#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;
}

VS2010では問題なく動作します。私は何が欠けていますか?

更新:私は急いでいて、コード全体を貼り付けませんでした。申し訳ありません:(

PS:タイトルが言うように、私が受け取るエラーは、「長さは定数式に表示できません」です。

4

1 に答える 1

1

あなたが抱えている問題がコンパイラのバグによるものなのか、それとも予想される動作なのかはわかりませんが、単純に static_cast を float に削除するだけで問題が解決するようで、まったく同じ値になります。

#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;
}
于 2012-08-19T22:50:56.500 に答える