vs2015 c++编译器规则:
- A template specialization for a specific type is more specialized than one taking a generic type argument.
- A template taking only T* is more specialized than one taking only T, because a hypothetical type X* is a valid argument for a T template argument, but X is not a valid argument for a T* template argument.(T*与T区别)
- const T is more specialized than T, because const X is a valid argument for a T template argument, but X is not a valid argument for a const T template argument.(const T与T区别)
- const T* is more specialized than T*, because const X* is a valid argument for a T* template argument, but X* is not a valid argument for a const T* template argument.(const T*与T*区别)
// partial_ordering_of_function_templates.cpp // compile with: /EHsc #include <iostream>
extern "C" int printf(const char*,...); template <class T> void f(T) { printf_s("Less specialized function called\n"); } template <class T> void f(T*) { printf_s("More specialized function called\n"); } template <class T> void f(const T*) { printf_s("Even more specialized function for const T*\n"); }
int main() { int i =0; const int j = 0; int *pi = &i; const int *cpi = &j;
f(i); // Calls less specialized function. f(pi); // Calls more specialized function. f(cpi); // Calls even more specialized function. // Without partial ordering, these calls would be ambiguous. } /** output Less specialized function called More specialized function called Even more specialized function for const T* */ |