我读了书<C++模板 - 完整指南>和学习指针的模板专业化.(也许我误解了这本书的这一部分)
(1)这是我的简单模板:
#include <iostream>
template<typename T>
void Function(const T& a)
{
std::cout << "Function<T>: " << a << std::endl;
}
template<typename T>
void Function<T*>(const T* a)
{
std::cout << "Function<T*>: " << a << std::endl;
}
int main(void)
{
Function(1);
Function(1.2);
Function("hello");
Function((void*)0x25);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用ubuntu16.04 x64,g ++ 5.3,编译器报告:
$ g++ main.cpp -o main.exe
main.cpp:10:29: error: non-type partial specialization ‘Function<T*>’ is not allowed
void Function<T*>(const T* a)
Run Code Online (Sandbox Code Playgroud)
(2)但这段代码是正确的:
#include <iostream>
template<typename T>
void Function(const T& a)
{
std::cout << "Function<T>: " << a << std::endl;
}
int main(void)
{
Function(1);
Function(1.2);
Function("hello");
Function((void*)0x25);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果显示:
$ g++ main.cpp -o main.exe
$ ./main.exe
Function<T>: 1
Function<T>: 1.2
Function<T>: hello
Function<T>: 0x25
Run Code Online (Sandbox Code Playgroud)
我的问题是:关于指针专业化的书是错的吗?或者我误解了书中这一部分的含义?或者是其他东西 ?
关于类中指针特化的更新.
(3)带指针特化的模板类:
#include <iostream>
template<typename T>
struct Base {
T member;
Base(const T& a)
: member(a)
{
}
void hello()
{
std::cout << member << std::endl;
}
};
template<typename T>
struct Base<T*> {
T* member;
Base(T* a)
: member(a)
{
}
void hello()
{
std::cout << member << std::endl;
}
};
int main(void)
{
Base<int> b1(12);
Base<double> b2(2.4);
Base<char*> b3("hello");
Base<void*> b4((void*)0x25);
b1.hello();
b2.hello();
b3.hello();
b4.hello();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个代码是正确的,有一个警告:
$ g++ main.cpp -o main.exe
main.cpp: In function ‘int main()’:
main.cpp:37:27: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
Base<char*> b3("hello");
^
$ ./main.exe
12
2.4
hello
0x25
Run Code Online (Sandbox Code Playgroud)
(4)没有指针专门化的模板类:
#include <iostream>
template<typename T>
struct Base {
T member;
Base(const T& a)
: member(a)
{
}
void hello()
{
std::cout << member << std::endl;
}
};
int main(void)
{
Base<int> b1(12);
Base<double> b2(2.4);
Base<char*> b3("hello");
Base<void*> b4((void*)0x25);
b1.hello();
b2.hello();
b3.hello();
b4.hello();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果是一样的:
$ g++ main.cpp -o main.exe
main.cpp: In function ‘int main()’:
main.cpp:39:27: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
Base<char*> b3("hello");
^
$ ./main.exe
12
2.4
hello
0x25
Run Code Online (Sandbox Code Playgroud)
这是否意味着指针专业化是不必要的?或者这个功能在不同的编译器上表现不同?
正如您已经被告知的那样,不允许对函数模板进行部分特化.你可以使用std::enable_if这个:
template <typename T, typename std::enable_if_t<!std::is_pointer<T>::value>* = 0>
void func(T val) { std::cout << val << std::endl; }
template <typename T, typename std::enable_if_t<std::is_pointer<T>::value>* = 0>
void func(T val) { func(*val); }
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找更简单的语法,请等待概念
错误消息告诉您出了什么问题:
Run Code Online (Sandbox Code Playgroud)non-type partial specialization ‘Function<T*>’ is not allowed
您只能部分专门化类型(类)。您尝试了部分专门化功能。函数不是类型;您只能完全专门化它们。