为什么这个函数模板特化不编译?
Why does this function template specialisation not compile?
我正在尝试在模板化 class 中专门化模板化函数。它工作正常,直到我添加专业化:然后它不再编译。
这是我正在尝试做的事情的简化示例:
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 returns 当我尝试编译它时:
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 不同,因为 link 他们试图 return 与模板不同的类型,这是一个非常特殊的情况,我不是尝试在这里做。)
[temp.expl.spec]/16 In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well... [ Example:
template <class Y> template <>
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
// its enclosing class template A is not
—end example ]
基本上,任何以 template<something> template</*nothing*/>
开头的内容都是错误格式的。
您的源代码不是有效的 C++,template<>
不能遵循模板参数列表。
模板 TestClass<TString, TStringStream, TChar>::convert
也可以完全特化,但仅限于 TestClass<TString, TStringStream, TChar>
的给定实例。如:
template <>
template <>
inline bool TestClass<RString, RStringstream, char>::convert<bool>(const char* text)
{
return text == "true";
}
我正在尝试在模板化 class 中专门化模板化函数。它工作正常,直到我添加专业化:然后它不再编译。
这是我正在尝试做的事情的简化示例:
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 returns 当我尝试编译它时:
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 不同,因为 link 他们试图 return 与模板不同的类型,这是一个非常特殊的情况,我不是尝试在这里做。)
[temp.expl.spec]/16 In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well... [ Example:
template <class Y> template <> void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but // its enclosing class template A is not
—end example ]
基本上,任何以 template<something> template</*nothing*/>
开头的内容都是错误格式的。
您的源代码不是有效的 C++,template<>
不能遵循模板参数列表。
模板 TestClass<TString, TStringStream, TChar>::convert
也可以完全特化,但仅限于 TestClass<TString, TStringStream, TChar>
的给定实例。如:
template <>
template <>
inline bool TestClass<RString, RStringstream, char>::convert<bool>(const char* text)
{
return text == "true";
}