I use gcc 4.7.3 for ARM platform to compile my code. I have several classes like this:
// types.h
enum Types
{
kType1,
kType2
// ...
};
// d1.h
class D1 : public Base
{
public:
static const int type = kType1;
// ...
};
// d2.h
class D2 : public Base
{
public:
static const int type = kType2;
// ...
};
Somewhere in the sources I use those classes:
MyObject obj;
doSomething<D1>(obj);
doSomething<D2>(obj);
// other.cpp
class Foo
{
template<typename T>
void doSomething(MyObject obj)
{
mm_.insert(std::multimap<int, MyObject>::value_type(T::type, obj));
}
};
And get the next messages (during linking):
undefined reference to `D1::kType`
undefined reference to `D2::kType`
// more messages of this type
OK. If I change do_something function like this:
template<typename T>
void doSomething(MyObject obj)
{
mm_.insert(std::multimap<int, MyObject>::value_type( (int) T::type, obj));
}
it compiles OK. But why? Can't find anything in the standard about it. Does anybody have ideas about what's going on?
Thanks.
P.S.
This fix
// d1.cpp
const int D1::kType;
also works, but this is expected.
P.P.S. the answer would be quite obvious in case of using reference or pointer to T::type, but I don't see anything that requires a ref or ptr. AFAIK std::multimap::value_type take the arguments by value (not ref, nor ptr).