模板型別推導

2021-08-20 18:24:36 字數 1820 閱讀 9622

param引用無const修飾

template

void f(t& param);

int x = 1 ;

const

int cx = x ;

const

int& rx = x ;

f(x) ;

f(cx);

f(rx);

函式呼叫

t的型別

param的型別

f(x)

intint&

f(cx)

const int

const int&

f(rx)

const int

const int&

param引用存在const修飾

template

void f(const t& param);

int x = 1 ;

const

int cx = x ;

const

int& rx = x ;

f(x) ;

f(cx);

f(rx);

函式呼叫

t的型別

param的型別

f(x)

intconst int&

f(cx)

intconst int&

f(rx)

intconst int&

param指標無const修飾

template

void f(t* param);

int x = 1 ;

const

int* px = &x ;

f(x) ;

f(px);

函式呼叫

t的型別

param的型別

f(x)

intint*

f(px)

const int

const int*

template

vodi f(t&& param);

int x = 1;

const

int cx= x ;

const

int& rx = x ;

f(x);

f(cx);

f(rx);

函式呼叫

t的型別

param的型別

f(x)

int&

int&

f(cx)

const int&

const int&

f(rx)

const int&

const int&

f(27)

int&&

int&&

template

void f(t param);

int x = 1;

const

int cx= x ;

const

int& rx = x ;

const

char* const px = "123";

f(x);

f(cx);

f(rx);

f(px);

函式呼叫

t的型別

param的型別

f(x)

intint

f(cx)

intint

f(rx)

intint

f(27)

intint

f(px)

const char*

const char*

模板型別推導 auto推導

effective modern c 果然是神書,乾貨滿滿,簡單記錄下。item1 模板推倒 典型的模板函式 temlate void fn paramtype param 要記住的東西 在模板型別推導的時候,有引用特性的引數的引用特性會被忽略 在推導通用引用引數的時候,左值會被特殊處理 在推導按值...

C 模板型別推導

內容參考 effective modern c 中的條款1 int x 27 const int cx x const int rx x const int p x 1.paramtype是個指標或引用,但不是個萬能引用 去引用不去const template void f t param f x ...

徹底搞定模板型別推導

templatevoid f t param t templatevoid f t param t templatevoid f t param just t對於paramtype是乙個指標或引用,expr型別 傳入的實參型別 去掉引用便是t型別。如 int x 27 x is an int f x...