Foo.hで:
class Foo
{
public:
Foo();
static const unsigned int FOOBAR = 10;
static const unsigned int BARFOO = 20;
private:
unsigned int m_FooBar;
bool m_Bar;
void Bar();
};
Foo.cpp:
Foo::Foo()
: m_FooBar(FOOBAR), // this works
m_Bar(false)
{
}
void Foo::Bar()
{
//m_FooBar = m_Bar ? FOOBAR : BARFOO; // linker fails *1
m_FooBar = FOOBAR; // ok
}
GCC 4.5.3 でコンパイルしています。行 *1 のコメントを外すとリンカーが失敗する理由はありますか?
Foo.o: In function 'Foo::Bar' (name unmangled):
Foo.cpp: undefined reference to `Foo::FOOBAR'
Foo.cpp: undefined reference to `Foo::BARFOO'
VC2005、2008、2010、CB2010で試しました。それらはすべて正常にコンパイルおよびリンクされました。この場合、GCC が失敗するのはなぜですか?
here の答えを考えると、他の一般的なコンパイラが GCC のように失敗しないのはなぜですか? いずれにせよ、GCC やその他の一般的なコンパイラのバグに違いありません。それとももっと合理的な説明がありますか?