C ++函数完全专业化给出错误
C++ Function full specialization giving error
我有以下代码来理解函数完全专业化的概念:
//Function Full specialization is legal but not partial
class Wrapper
{
public:
void setValue(int x) { }
};
template <typename R, typename T>
R* create(T t)
{
return new R(t);
}
template <>
Wrapper* create<Wrapper, int>(int n) // fully specialized now -> legal...
{
Wrapper* w = new Wrapper();
w->setValue(n);
return w;
}
//template <typename T>
//Wrapper* create<T, int>(T n) // partial specialized now -> illegal...
//{
// Wrapper* w = new Wrapper();
// w->setValue(n);
// return w;
//}
//T
int main()
{
create< Wrapper, int>(2);
create< int, int>(2);
}
上面的代码可以正常编译和执行,但是当我将完整的专业化函数签名更改为其他内容时:
template <>
const char* create<const char*, int>(int n) // fully specialized now -> legal...
{
//Wrapper* w = new Wrapper();
//w->setValue(n);
//return w;
return "Hi";
}
或
template <>
char* create<char, char>(int n) // fully specialized now -> legal...
{
return (char*)"HI";
}
错误:
explicit specialization 'const char *create<const char*,int>(int)' is not a specialization of a function template Specialization and Overloading
explicit specialization 'char *create<char,char>(int)' is not a specialization of a function template Specialization and Overloading
为什么代码会报错,如何解决?
template <>
char* create<char, char>(int n)
{
return (char*)"HI";
}
和
template <>
const char* create<const char*, int>(int n)
{
return "Hi";
}
不是模板特化:前者没有一致的参数,而后者没有一致的 return 类型。
template<>
char* create<char, char>(char n) // char n instead of int n
{
return (char*)"HI";
}
这是一个可能的模板专业化:
template <>
const char** create<const char*, int>(int n) // const char** instead of const char*
{
static const char* test="Hi";
return &test;
}
我有以下代码来理解函数完全专业化的概念:
//Function Full specialization is legal but not partial
class Wrapper
{
public:
void setValue(int x) { }
};
template <typename R, typename T>
R* create(T t)
{
return new R(t);
}
template <>
Wrapper* create<Wrapper, int>(int n) // fully specialized now -> legal...
{
Wrapper* w = new Wrapper();
w->setValue(n);
return w;
}
//template <typename T>
//Wrapper* create<T, int>(T n) // partial specialized now -> illegal...
//{
// Wrapper* w = new Wrapper();
// w->setValue(n);
// return w;
//}
//T
int main()
{
create< Wrapper, int>(2);
create< int, int>(2);
}
上面的代码可以正常编译和执行,但是当我将完整的专业化函数签名更改为其他内容时:
template <>
const char* create<const char*, int>(int n) // fully specialized now -> legal...
{
//Wrapper* w = new Wrapper();
//w->setValue(n);
//return w;
return "Hi";
}
或
template <>
char* create<char, char>(int n) // fully specialized now -> legal...
{
return (char*)"HI";
}
错误:
explicit specialization 'const char *create<const char*,int>(int)' is not a specialization of a function template Specialization and Overloading
explicit specialization 'char *create<char,char>(int)' is not a specialization of a function template Specialization and Overloading
为什么代码会报错,如何解决?
template <>
char* create<char, char>(int n)
{
return (char*)"HI";
}
和
template <>
const char* create<const char*, int>(int n)
{
return "Hi";
}
不是模板特化:前者没有一致的参数,而后者没有一致的 return 类型。
template<>
char* create<char, char>(char n) // char n instead of int n
{
return (char*)"HI";
}
这是一个可能的模板专业化:
template <>
const char** create<const char*, int>(int n) // const char** instead of const char*
{
static const char* test="Hi";
return &test;
}