【问题标题】:G++ raise compile errors instead of warnings for narrowing conversionsG++ 引发编译错误而不是警告以缩小转换范围
【发布时间】:2021-07-31 13:17:35
【问题描述】:

我想得到编译错误而不是这段代码的警告:

#include <iostream>

int main(int argc, char ** argv)
{
    float a = 1.3f;
    int b = 2.0 * a;

    std::cout << b << "\n";
}

如果我编译它:

g++ test.cpp -o test

我没有错误。

但如果我用以下代码编译相同的代码:

g++ test.cpp -o test -Wconversion

我收到以下警告:

test.cpp: In function ‘int main(int, char**)’:
test.cpp:6:17: warning: conversion from ‘double’ to ‘int’ may change value [-Wfloat-conversion]
6 |     int b = 2.0 * a;

我正在寻找一种方法来获取 编译错误 而不是 warnings 仅针对这种特定类型的警告。

Obs.1:-Werror 可以使所有警告变成错误,但这不是我要找的

Obs.2:我正在使用g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

【问题讨论】:

    标签: c++ g++ narrowing


    【解决方案1】:

    使用-Werror= 仅将特定警告视为错误:

    g++ test.cpp -o test -Werror=conversion
    

    【讨论】:

      猜你喜欢
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 2012-10-06
      相关资源
      最近更新 更多