静态字段模板特化的"未定义引用"

Lor*_*one 4 c++ linker static templates

我有一个模板类头文件,它只有静态函数和字段.

template<typename T> class Luaproxy {

    static std::map<std::string, fieldproxy> fields;
    static const char * const CLASS_NAME;
    static void addfields();
    static int __newindex(lua_State * l){
        //implemented stuff, references to fields...
    }
    //etc
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,只会声明一些函数,因为我打算用模板特化来实现它们.

在.ccp文件中我有:

struct test { int a; }
template<> map<string, fieldproxy> Luaproxy<test>::fields;
template<> const char * const Luaproxy<test>::CLASS_NAME=typeid(test).name();
template<> void Luaproxy<test>::addfields(){
    //stuff, references to fields...
}
Run Code Online (Sandbox Code Playgroud)

Luaproxy<test>::fields从头文件中实现的函数和仅专门用于.cpp的函数中获取未定义的引用错误.请注意,Luaproxy<test>::CLASS_NAMELuaproxy<test>::addfields似乎在联系被发现.

是什么让map这么特别?

Lor*_*one 5

我终于设法让它工作了,但我无法真正说出为什么我的编译器(gcc 4.6.1)需要这样: template<> std::map<std::string, fieldproxy> Luaproxy<test>::fields=std::map<std::string, fieldproxy>();

显式构造函数似乎说服gcc有效地发出变量.我要求#gcc澄清,但不幸的是,该频道总是保持沉默.