I read the book <C ++ Templates - A Complete Guide> and learned the specialization of the template for a pointer. (maybe I misunderstand this part of the book)
(1) Here is my simple template:
#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; }
I am using ubuntu16.04 x64, g ++ 5.3, compiler report:
$ 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)
(2), but this code is correct:
#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; }
The result shows:
$ g++ main.cpp -o main.exe $ ./main.exe Function<T>: 1 Function<T>: 1.2 Function<T>: hello Function<T>: 0x25
My question is: is the pointer specialization book wrong? Or am I misunderstanding the meaning of this part in the book? Or something else?
Updating pointer specialization in a class.
(3) template class with pointer specialization:
#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; }
this code is correct with one warning:
$ 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
(4) template class without pointer specialization:
#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; }
Result
same:
$ 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
Does this mean that pointer specialization is useless? Or maybe this function works differently on different compilers?