C++: warning: passing ‘A’ chooses ‘int’ over ‘unsigned int’ [-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.

解决

强制转换:// a << (int) c;

Original: https://blog.csdn.net/qq_36428903/article/details/128715152
Author: mzhan017
Title: C++: warning: passing ‘A’ chooses ‘int’ over ‘unsigned int’ [-Wsign-promo]

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/812414/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球