I have an issue with template specialization which I would like to understand. I\'m working with Visual C++ 10.0 (2010). I have a class like this:
class Variable
Your analysis is correct - an explicitly specialized function template that has any template parameters specified with explicit values provides a complete definition of a function.
If you have properly included the corresponding .cpp
file that contains the explicit specialization's definition into your project, then VC++ should not raise a linker error. For Standards compliance though, let me note that you have to declare your specialization outside of the enclosing class. The Standard forbids to declare explicit specializations inside of the enclosing class (and other compilers will reject your code). So change the header file to declare the specialization like this, instead
class VariableManager
{
public:
template
VarT get(std::string const& name) const
{
// Some code...
}
private:
std::map mVariables;
};
// This method supposed to be fully evaluated, linkable method.
template<>
std::string VariableManager::get(std::string const& name) const;
Let me also note that you can't call get
inside of your class body. That's because any such call would not yet see the explicit specialization declaration, and would hence try to instantiate the function from the template definition. The Standard renders such code ill-formed, without a diagnostic being required.