gpt4 book ai didi

c++ - QT Creator 中没有警告

转载 作者:行者123 更新时间:2023-11-30 03:35:42 26 4
gpt4 key购买 nike

我在我的程序中几乎看不到任何警告。

我的cpp文件:

#include <iostream>

using namespace std;

int main()
{
long long int ll = 100000000067;
unsigned short sh = ll; //no warning here, why?

cout << sh << " " << ll << endl;


int s; //warning only here: warning: unused variable ‘s’ [-Wunused-variable]

return 0;
}

我的专业文件:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

QMAKE_CXXFLAGS += -Wall -Wextra -pedantic

我尝试将项目与 cmake 一起使用,但结果是一样的。

最佳答案

根据 GCC 文档

-Wconversion

Warn for implicit conversions that may alter a value. This includes conversions between real and integer, like abs (x) when x is double; conversions between signed and unsigned, like unsigned ui = -1; and conversions to smaller types, like sqrtf (M_PI). Do not warn for explicit casts like abs ((int) x) and ui = (unsigned) -1, or if the value is not changed by the conversion like in abs (2.0). Warnings about conversions between signed and unsigned integers can be disabled by using -Wno-sign-conversion.For C++, also warn for confusing overload resolution for user-defined conversions; and conversions that never use a type conversion operator: conversions to void, the same type, a base class or a reference to them. Warnings about conversions between signed and unsigned integers are disabled by default in C++ unless -Wsign-conversion is explicitly enabled.

对我来说,您使用 -Wconversion 生成的示例

~/main.cpp:9: warning: conversion to 'short unsigned int' from 'long long int' may alter its value [-Wconversion]
unsigned short sh = ll; //no warning here, why?
^

关于c++ - QT Creator 中没有警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41086870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com