【发布时间】:2017-05-15 13:12:25
【问题描述】:
N3690,第 14.8.2 节第 3 段有这个令人兴奋的例子:
template <class Z> void h(Z, Z*);
// #5: function type is h(int, const int*)
h<const int>(1,0);
问题:为什么不是h(const int, const int*)?
据了解,Z = const int,所以模板声明中每次出现的Z 都可以读作const int,还是我遗漏了什么?为什么指针不一样?我记得当参数有T& 或T* 时,它会保留T 的cv-qualifiers,但我看不出有任何可能在这里应用它。
【问题讨论】:
-
我不认为函数签名有
const参数这样的东西。任何未通过引用传递的参数都将被复制;提供的参数的constness 在那里无关紧要。const只适用于定义,是一个实现细节。
标签: c++ templates c++14 language-lawyer