What should happen to template class static member variables with definition in the .h file

前端 未结 3 486
星月不相逢
星月不相逢 2020-12-09 05:24

If a template class definition contains a static member variable that depends on the template type, I\'m unsure of what the reliable behavior should be?

In my case i

3条回答
  •  感动是毒
    2020-12-09 06:21

    The linker handles such cases almost exactly the same as for non-template class static members with __declspec(selectany) declaration applied, like this:

    class X {
    public:
    X(int i){};
    };
    __declspec(selectany) X x(1);//works in msvc, for gcc use __attribute__((weak))
    

    And as msdn says: "At link time, if multiple definitions of a COMDAT are seen, the linker picks one and discards the rest... For dynamically initialized, global objects, selectany will discard an unreferenced object's initialization code, as well."

提交回复
热议问题