为什么 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 e446 代码 initilizer effect Warning 抱怨 为什么 side in      更新时间:2023-10-16

我需要帮助理解为什么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不会警告您。它会让你做到的。