Walton1128/CPP-Templates-2nd--

类型推断中的类型转换

Opened this issue · 1 comments

b-y-y commented

如果调用参数是按值传递的,那么只有退化(decay)这一类简单转换是被允许的:const 和 volatile 限制符会被忽略,引用被转换成被引用的类型,raw array 和函数被转换为相 应的指针类型。通过模板类型参数 T 定义的两个参数,它们实参的类型在退化(decay) 后必须一样。
粗体部分应该是:通过模板类型参数 T 定义的两个参数,它们的退化类型(ignore const/raw array->pointer)必须一样。
template
bool cmp( T t1, T t2 )
{
return *t1 < *t2;
}
int main()
{
int arr[ ] = { 1, 2,3 };
const int * p1 = arr;
int * p2 = arr;//lead toerror
const int * p2 = arr;//fine
cmp( p1, p2 );
}

粗体部分这样描述感觉更合适:
通过模板类型参数 T 定义的两个参数,实参的退化(decay)类型必须一样。