模板類型推斷、auto和decltype

template type deduction

reference or pointer

int x = 27; 
const int cx = x;
const int& rx = x; 

template<typename T>
void f(T& param); 
  • int : int&
  • const int : const int&
  • const int : const int&

template<typename T>
void f(const T& param);
  • int : int&
  • int : const int&
  • int : const int&

Universal Reference

template<typename T>
void f(T&& param);
  • int& : int&
  • const int& : const int&
  • const int& : const int&
  • f(27) -> int : int&&

Neither a Pointer nor a Reference

  • int : int
  • int : int
  • int : int
  • 數組作爲參數按值傳遞時候,會轉換爲指針

auto

just like the T.

decltype

和表達式完全相同的類型

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章