我有一個模板專業化問題,我想了解。我正在使用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;
};
從理論上講,因爲我的專業「獲取」的方法,連接器應該能夠從一個目標文件回升。相反,我得到的鏈接未解決的引用錯誤,如果我把源文件的方法:
template<>
std::string VariableManager::get<std::string>(std::string const& name) const
{
// Doing something...
}
如果我把這個方法在頭文件內聯,構建去就好了。我不明白,模板功能,因爲這:
template<typename VarT>
VarT get(std::string const& name) const;
應放置在頭部,因爲編譯器將不能夠根據調用代碼,專門的模板,但完全專業化的情況下,它是類「實現,因此專用模板方法應該已經作爲公共符號存在。有人可以提出這個問題嗎?
有趣,但似乎標準合規問題是問題所在。我刪除了課堂上的聲明,並按照您的建議進行了專業化聲明,並且鏈接器沒有任何問題。應該記住,專業化必須在課堂範圍之外宣佈。感謝您的親切幫助! – progician
對我來說,找到你的答案真是太幸運了。對於我來說,如果我將模板特化放在類中,而鏈接器無法找到它,那麼編譯器的工作仍然是一個謎。但是,你絕對保存了我的一天! – Nipheris