为什么 lint 抱怨 "Warning 446: side effect in initilizer" lint e446 下面的代码?
Why does lint complain "Warning 446: side effect in initilizer", lint e446 for the code below?
我需要帮助理解为什么lint由于struct
初始化而在下面的代码中抱怨。代码运行时没有任何问题,或者至少没有任何已知的问题
struct MsgKey_t {
int type;
int index;
int signal;
};
typedef std::map< int, std::pair< MsgKey_t*, int* > > MyMap_t;
MyMap_t myMap;
MyMap_t::iterator subKey = myMap.find( 11 );
if ( myMap.end() == subKey )
{
exit(-1);
}
MsgKey_t key = { subKey->second.first->type, subKey->second.first->index, subKey->second.first->signal };
如果我将结构的初始化更改为:
MsgKey_t key;
memcpy( &key, subKey->second.first, sizeof( key ) );
或:
MsgKey_t* pKey = subKey->second.first;
MsgKey_t key = { pKey->type, pKey->index, pKey->signal };
林特整天都很开心。
lint是否在初始化中引用了operator ->
?这可能会产生副作用,具体取决于迭代器的实现。
根据C++lint文档,原因如下:
自动聚合(数组、结构和并集)通常由常值表达式的集合初始化副作用。编译器可以支持副作用,在这种情况下可能要取消显示此消息。
完全有可能这些不是恒定的,可能会产生副作用:
subKey->second.first->type, subKey->second.first->index, subKey->second.first->signal
由于memcpy
在技术上不遵循典型的初始化,只是四处移动字节,lint不会警告您。它会让你做到的。
相关文章:
- 是否有 lint 工具可以检查子类虚拟函数是否与父类定义匹配?
- Pc Lint,如何使用 init() 抑制类的 err 613(可能使用空 ponter)
- 如何从 Pc-lint 中排除特定'TI'包装路径?
- Visual Lint?中的每个项目选项
- PC lint/Flexelint规则反对纯炭
- FlexeLint/PC lint警告说,不考虑std::string::append的返回值
- 我可以在 lint 使用 boost 的 ptr_vector.hpp 的文件吗?
- 如何告诉lint跟踪指向向量的托管指针
- Lint更喜欢在std:string上使用erase()方法,而不是clear()方法
- 如何禁用PC lint中静态断言的评估
- clang格式破坏lint注释
- 迭代器使用-Lint警告
- 如何使用make运行增量PC-Lint ?
- 使用std::unary_function时发出Lint警告
- Pc Lint + Eigen library
- 我在这个lint错误抑制尝试中做错了什么?有没有更好的方法
- 为什么 lint 抱怨 "Warning 446: side effect in initilizer" lint e446 下面的代码?
- 排除PC Lint中的所有消息
- Lint警告559的问题