相关疑难解决方法(0)

模板专门化在链接时失败

我有模板专业化的问题,我想了解.我正在使用Visual C++ 10.0(2010).我有一个这样的课:

class VariableManager
{
public:

    template<typename VarT>
        VarT get(std::string const& name) const
        {
                    // Some code...
        }

   // This method supposed to be fully evaluated, linkable method.   
    template<>
        std::string get<std::string>(std::string const& name) const;

private:
    std::map<std::string, boost::any> mVariables;
};
Run Code Online (Sandbox Code Playgroud)

理论上,因为我专门使用"get"方法,所以链接器应该能够从目标文件中获取.相反,如果我将方法放在源文件中,我会得到一个未解决的链接器引用错误:

    template<>
    std::string VariableManager::get<std::string>(std::string const& name) const
            {
                 // Doing something...
            }
Run Code Online (Sandbox Code Playgroud)

如果我将此方法作为内联放在头文件中,那么构建就可以了.我明白模板的功能如下:

        template<typename VarT>
            VarT get(std::string const& name) const;
Run Code Online (Sandbox Code Playgroud)

应该放在标题中,因为编译器将无法根据调用代码专门化模板,但在完全特化的情况下,它是类的实现,因此专用模板方法应该已经存在公共符号.有人可以对这个问题有所了解吗?

c++ templates visual-c++

8
推荐指数
1
解决办法
2164
查看次数

标签 统计

c++ ×1

templates ×1

visual-c++ ×1