T&
template<typename T> void f(T & param); // 咱们声明以下变量 int x = 27; const int cx = x; const int& rx = x;
函数调用时,推导出的Param和T的类型以下:ios
f(x); // T is int, param's type is int& f(cx); // T is const int, param's type is const int& f(rx); // T is const int, param's type is const int&
须要特别注明的是,经过T&
的方式传入数组,数组的大小信息不会丢失。数组
template<typename T> void f(T& param); int arr[10]; f(arr); // T is const int[10], param type is const int(&)[10]
在类型推导期间,数组和函数将退化为指针类型,除非他们是被初始化为引用。函数
const T&
template<typename T> void f(const T& param); int x = 27; const int cx = x; const int& rx = x;
在进行类型推导的时候,rx的引用性被忽略了。spa
f(x); // T is int, param's type is const int& f(cx); // T is int, param's type is const int& f(rx); // T is int, param's type is const int&
template<typename T> void f(T* param); // param is now a pointer int x = 27; const int* px = &x; f(&x); // T is int, param's type is int * f(px); // T is const int, param's type is const int *
template<typename T> void f(T&& param); // param is now a universal reference int x = 27; const int cx = x; const int rx = x; f(x); // x is lvalue, so T is int&, param's type is also int& f(cx); // cx is lvalue, so T is const int&, param's type is also const int& f(rx); // rx is lvalue, so T is const int&, param's type is also const int& f(27); // 27 is rvalue, so T is int, param's typs is int&&
template<typename T> void f(T param);
当ParamType既不是指针也不是引用的时候,咱们按照值传递的方式进行处理。
须要举出一个有用的例子:指针
template<typename T> void f(T param); const char* const ptr = "hello world\n"; f(ptr); // param's type is const char*
auto 类型对象推导一般和模板类型推导是相同的。
例子:code
const char name[] = "zhouyang"; auto arr1 = name; // arr1's type is const char* auto& arr2 = name; // arr2's type is const char(&)[9] void someFunc(int, double); // someFunc is a function auto func1 = someFunc; // func1's type is void(*)(int, double) auto& func2 = someFunc; // func2's type is void(&)(int, double)
惟一的例外是:使用auto和大括号进行初始化时,自动推导为std::initializer_list。而且,对于使用括号进行的初始化,模板类型推导会失败。对象
decltype 通常状况下老是返回变量名或者表达式的类型而不作任何的修改。get
const int i = 0; // decltype(i) is const int bool f(const Widget& w) // decltype(w) is const Widget& Widget W; // decltype(w) is Widget
在C++14中,提供了decltype(auto)的支持,它从初始化式子中推导类型,使用的是decltype的推导规则。编译器
Widget w; cosnt Widget& cw = w; auto myWidget1 = cw; // myWidget1's type is Widget decltype(auto) myWidget2 = cw; // decltype type deduction: // myWidget2's type is const Widget& // 注:能够在模板中使用
特例:it
#include <iostream> using namespace std; int main() { int temp = 10; decltype((temp)) temp1 = temp; // temp1's type is int& temp1 = 1; cout<< temp << endl; return 0; } //输出 : 1
能够利用编译器诊断来完成。咱们想要知道被推导出的类型,能够首先声明一个类模板,可是不定义它。那么编译器的出错信息会包含推导的类型信息。
template<typename T> class TD;
经过编译器内置的宏定义,能够输出函数类型
#include <iostream> #include <vector> using namespace std; void test_func(int) { #if defined(__GNUC__) cout << __PRETTY_FUNCTION__ << endl; #elif defined(_MSC_VER) cout << __FUNCSIG__ << endl; #endif } int main() { test_func(10); return 0; }