返回“常量布尔值”会导致警告 C4180
Return of `const bool` causes warning C4180
struct CCompare
{
const bool operator()(const int& lhs, const int& rhs) const {
return lhs < rhs;
}
};
警告1 警告 C4180:应用于函数类型的限定符没有 意义;
我在编程书中看到返回值的用法const bool
。当我使用 vs2010 编译上述代码时,它报告警告 C4180。
相反,以下代码不会引起相同的警告。
struct CCompare
{
bool operator()(const int& lhs, const int& rhs) const {
return lhs < rhs;
}
};
问题1>使用const Fundamental_Data_Types
作为函数返回值真的没有意义吗?
问题2>使用const Type
作为函数返回值仅在类型是类/结构时才有意义吗?
谢谢
//更新//
struct CClass
{
int val;
CClass(int _val) : val(_val) {}
void SetValue(int _val) {
val = _val;
}
};
struct CCompare
{
const CClass getMe() const {
return CClass(10);
}
CClass getMeB() const {
return CClass(10);
}
};
int main(/*int argc, char *argv[]*/)
{
CCompare c;
c.getMe().SetValue(20); // error
c.getMeB().SetValue(20); // ok
}
是的
,对你的两个问题都是肯定的。 返回值为右值和 CV 限定符仅适用于具有类类型。
原因很简单:通常什么都没有你可以用一个右值来做常量会做出区别——毕竟它是一个值,而不是一个对象。对于类类型,有一些成员函数要纳入帐户(这意味着您可以从rvalue(,所以恒常性突然变得相关。
相关文章:
- 警告处理为错误这里有什么问题
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- cppcheck在const std::string[]上引发警告
- GCC对可能有效的代码抛出init list生存期警告
- 如何在BST的这个简单递归实现中消除警告
- 关于std::move的使用,是否有编译警告
- g++ 在某个类成员未初始化时不发出警告
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- 使用typeid警告未使用的变量
- 示例C++项目编译中的警告
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 如何修复编译器警告 C6386 和 C6385?
- 返回语句后的代码,没有警告
- 获取隐式转换溢出从无符号到已签名的警告
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 在未链接的部分上生成警告
- 警告 C4552:">>":未使用表达式的结果
- 禁止显示由于常量为零而比较始终为假的警告
- 警告 C4180:应用于函数类型的限定符没有意义;忽视
- 返回“常量布尔值”会导致警告 C4180