c++ - Template specialization fails on linking -
c++ - Template specialization fails on linking -
i have issue template specialization understand. i'm working visual c++ 10.0 (2010). have class this:
class variablemanager { public: template<typename vart> vart get(std::string const& name) const { // code... } // method supposed evaluated, linkable method. template<> std::string get<std::string>(std::string const& name) const; private: std::map<std::string, boost::any> mvariables; };
in theory, because specialized "get" method, linker should able pick object file. instead, unresolved reference error linker if place method in source file:
template<> std::string variablemanager::get<std::string>(std::string const& name) const { // doing something... }
if place method in header file inline, build goes fine. understand template functions this:
template<typename vart> vart get(std::string const& name) const;
should placed in header because compiler won't able specialize template according calling code, in case of total specialization class' implementation specialized template method should exist public symbol. throw lite on issue?
your analysis right - explicitly specialized function template has template parameters specified explicit values provides finish definition of function.
if have included corresponding .cpp
file contains explicit specialization's definition project, vc++ should not raise linker error. standards compliance though, allow me note have declare specialization outside of enclosing class. standard forbids declare explicit specializations within of enclosing class (and other compilers reject code). alter header file declare specialization this, instead
class variablemanager { public: template<typename vart> vart get(std::string const& name) const { // code... } private: std::map<std::string, boost::any> mvariables; }; // method supposed evaluated, linkable method. template<> std::string variablemanager::get<std::string>(std::string const& name) const;
let me note can't phone call get<std::string>
within of class body. that's because such phone call not yet see explicit specialization declaration, , hence seek instantiate function template definition. standard renders such code ill-formed, without diagnostic beingness required.
c++ templates visual-c++
Comments
Post a Comment