前几天我看到这段代码:
template< class T >
T findMax(const T const * data,
const size_t const numItems) {
// Obtain the minimum value for type T
T largest =
std::numeric_limits< T >::min();
for(unsigned int i=0; i<numItems; ++i)
if (data[i] > largest)
largest = data[i];
return largest;
}
为什么每个参数都包含两个const
?
最佳答案
这没有任何有意义的理由。此外,这种显式重复 const
限定符在 C++ 中是非法的。代码格式错误。
7.1.6.1 The cv-qualifiers [dcl.type.cv]
1 There are two cv-qualifiers, const and volatile. Each cv-qualifier shall appear at most once in a cvqualifier-seq.
可以在 C++ 声明中引入冗余的 const
,但这需要在 typedef 名称中“隐藏”之前的 const
。在这种形式下,声明将是合法的,多余的限定符将被忽略。
关于c++ - 参数声明中的 "const T const",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41883379/