I have a weird behaviour with static integal constants:
#include <iostream>
#include <inttypes.h>
class Test{
public:
static const uint32_t Magic = 0x1123;
};
class DataStream{
public:
template<typename T>
DataStream& operator <<( const T& value )
{
std::cout << value << std::endl;
return *this;
}
};
int main()
{
DataStream s;
uint32_t a = Test::Magic; // ok
bool compare = ( a == Test::Magic ); // ok
s << compare;
s << a;
s << Test::Magic; // fail
return 0;
}
I know that such constants should be defined outside of class in .cpp as
const uint32_t Test::Magic;
But strange things is that code above work fine with out line s << Test::Magic;
and produce error only if Magic
used with template operator <<
directly.
Even more error undefined reference to 'Test::Magic'
appear with GCC
, but not with MSVC
.
The question is why I should define Test::Magic
outside of class (even without value!!!), and why my code work fine in some conditions even without such definition?