【发布时间】:2015-10-02 14:21:40
【问题描述】:
我正在尝试在模板化类中专门化模板化函数。在我添加专业化之前它工作正常:然后它不再编译。
这是我正在尝试做的事情的简化示例:
template <typename TString, typename TStringStream, typename TChar>
class TestClass
{
public:
template <typename T>
static T convert(const TChar* text);
};
//This specialisation doesn't compile
template <typename TString, typename TStringStream, typename TChar>
template <>
inline bool TestClass<TString, TStringStream, TChar>::convert(const TChar* text)
{
return strcmp(text, "true");
}
template <typename TString, typename TStringStream, typename TChar>
template <typename T>
T TestClass<TString, TStringStream, TChar>::convert(const TChar* text)
{
TStringStream textStream(text);
T result;
textStream >> result;
return result;
}
void main()
{
TestClass<RString, RStringstream, char>::convert<bool>("0");
}
这是我尝试编译时 Visual Studio 2010 返回的编译器错误:
error C2244: 'TestClass<TString,TStringStream,TChar>::convert' : unable to match function definition to an existing declaration
definition
'bool TestClass<TString,TStringStream,TChar>::convert(const TChar *)'
existing declarations
'T TestClass<TString,TStringStream,TChar>::convert(const TChar *)'
我在这里做错了什么?
(这个问题与this one 不同,因为在该链接中,他们试图返回与模板不同的类型,这是一种非常特殊的情况,我不想在这里做。)
【问题讨论】:
-
我的问题有所不同,因为在该链接中,他们试图返回与模板不同的类型,这是一种非常特殊的情况,我不想在这里做。
-
错了,别这样
void main -
lol 只是为了示例而快速输入,显然这段代码是更大整体的一部分:)
标签: c++ templates template-specialization