C++: warning: passing ‘A’ chooses ‘int’ over ‘unsigned int’ [-Wsign-promo]

文章目录

  • 示例代码
  • 解释
  • Wsign-promo

示例代码

#include 
using namespace std;

typedef enum {
        NULL,
} TYPE;
int main()
{
        ostrstream a;
        TYPE c = NULL;
        a<<c;
        return 0;
}

解释

这个警告的原因是,strstream类在做输出的时候,选择了一个转换的重载函数,会将 unsigned int 类型转换成带符号的int。这样其实是比较危险的操作。如果只是一个打印语句,也行无所谓。

Wsign-promo

-Wsign-promo (C++ and Objective-C++ only)
Warn when overload resolution chooses a promotion from unsigned or enumerated type to a signed type, over a conversion to an unsigned type of the same size. Previous versions of G++ tried to preserve unsignedness, but the standard mandates the current behavior.

你可能感兴趣的:(c/c++,c++)