Template specialization fails on linking

前端 未结 3 1305
一生所求
一生所求 2021-02-15 12:10

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         


        
相关标签:
3条回答
  • 2021-02-15 12:33

    Specializing a template doesn't force the compiler to instantiate it (I think that GCC does though); you still need to explicitly tell the compiler to actually instantiate the template. You can do so with explicit template instantiation. Basically, just add this in the source file:

    template std::string VariableManager::get(const std::string& name) const;
    
    0 讨论(0)
  • 2021-02-15 12:47

    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<typename VarT>
            VarT get(std::string const& name) const
            {
                        // Some code...
            }
    
    private:
        std::map<std::string, boost::any> mVariables;
    };
    
    // This method supposed to be fully evaluated, linkable method.   
    template<>
    std::string VariableManager::get<std::string>(std::string const& name) const;
    

    Let me also note that you can't call get<std::string> 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.

    0 讨论(0)
  • 2021-02-15 12:55

    Method starting with template<> is still considered a template specialization method. So you must have to put into a header file.

    If you want to put it into a implementation file then you have to overload it.

    class VariableManager
    {
    //...
      VarT get(std::string const& name) const
      {}
    
      std::string get(std::string const& name) const; //overloading not specialization
    };
    
    0 讨论(0)
提交回复
热议问题