c++ - 参数声明中的 "const T const"

标签 c++

前几天我看到这段代码:

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/

相关文章:

c++ - 运行来自 Learning Opencv 的练习 2-4 的问题,O'Reilly

c++ - undefined symbol __istype with/opt/local/bin/g++?

c++ - 通过Qt Application约束其他应用程序的窗口大小

c++ - 如何将 com 实例转换为变体,以在 idispach 调用中传递它

c++ - 如何使回调全局化以便我可以将它用于其他函数?

c++ - 我可以从函数返回 printf 语句吗? C++

c++ - 为什么 std::string_view 在三元表达式中创建悬空 View ?

c++ - 我们可以专门化类模板的枚举(类型)成员吗?

c++ - 使用编译时间常量抛出错误

C++ 转换字节