AnsweredAssumed Answered

c++ template instantiation & linker

Question asked by AndreyZ on Apr 9, 2014
Latest reply on Oct 12, 2014 by AndreyZ

Hi guys, i'm used to try using templates in my project until i started to get the messages from the linker about multiply defined symbols

I found that compiler includes cpp files into other files by default.

but i want to use explicit instatiation.

 

LexicalMaster.hxx:

template<typename StringType>

class LexicalMaster

{

     static const std::map<int,StringType> &_member;

     static const std::map<int,StringType> &Init()

     {

          static std::map<int,StringType> _tmp;

          _tmp.insert(std::map<int,StringType>::value_type(1,"example1"));

          _tmp.insert(std::map<int,StringType>::value_type(2,"example2"));

          return _tmp;

     }

//...

};

LexicalMaster.cxx:

template<typename StringType>

const std::map<int,StringType> &LexicalMaster<StringType>::_member = LexicalMaster<StringType>::Init();

 

Ok, the compiler switch "-no-implicit-inclusion" prevents inclusion of the LexicalMaster.cxx to other files, but now i can't get any effect using any following expressions:

 

LexicalMaster.cxx:

template class LexicalMaster<std::string>; //ISO/IEC 14882:2003 style

or

#pragma instantiate class LexicalMaster<std::string> //visualdsp style

>process build datetime

.\Source\LexicalMaster.cxx

...

Linking...

[Error li1021]  The following symbols referenced in processor 'p0' could not be resolved:

'std::map<int,StringType> &LexicalMaster<T1>::_member [with T1=string] [_Trim__29LexicalMaster__tm__9_c6stringSFRZ2ZPCZ1Z_RZ2Z]' referenced from '.\Obj\...\SomeFile.doj'

 

also a found that anyting typed at these strings affects nothing, even if it's a garbage:

tempsdsdlate class LexicalMaster<std::string::value_type,std::string>;            //wtf?

#pragmswa3da instantiate LexicalMaster<std::string::value_type,std::string>   //wtf?

>process build datetime

.\Source\LexicalMaster.cxx

...

Linking...

[Error li1021]  The following symbols referenced in processor 'p0' could not be resolved:

'std::map<int,StringType> &LexicalMaster<T1>::_member [with T1=string] [_Trim__29LexicalMaster__tm__9_c6stringSFRZ2ZPCZ1Z_RZ2Z]' referenced from '.\Obj\...\SomeFile.doj'

 

Other.cxx just uses LexicalMaster<std::string> but i got the linker error about unresolved symbol

 

ps: i edited linker error to contain "std::map<int,StringType>" just because i dont whant to care about std::map full name in this post

 

please help to understand whats going on with this compiler

best regards,

Andrey Z

Outcomes