是明智的,忽略gcc / clang的“-Wmissing-braces”警告? [英] Is it wise to ignore gcc/clang's "-Wmissing-braces" warning?

查看:3255
本文介绍了是明智的,忽略gcc / clang的“-Wmissing-braces”警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下程序:

  #include< array& 

int main()
{
std :: array< int,1> x = {0}; // 警告!
x = {{0}} // no warning
return 0;
}

第一次初始化导致gcc上的警告4.7.2 ...



  main.cpp:5:22:warning:unused variable'x'[-Wunused-variable] 

...和clang 3.1

  main.cpp:5:28:warning:建议大括号初始化subobject [-Wmissing-braces] 
std :: array< int,1> x = {0};

对于标准来说,双花括号或单花括号之间应该没有区别



有两种方法处理警告:



这是什么意思?你建议? IMHO,双卷曲表情看起来有点丑陋。另一方面,警告可能在更复杂的示例中检测到实际问题。你知道警告会帮助你的例子吗?

解决方案

-Wall 中将不再启用.org / bugzilla / show_bug.cgi?id = 25137> -Wmissing-braces $ c>(对于C ++模式),从4.8开始,正是你描述的原因。对于当前版本的GCC,禁用或忽略警告,你所编写的代码是按照它的方式编写的。 / p>

警告可能意在覆盖

  struct A {int a; int b; }; 
struct B {A a; int b; };
B b = {
1,
2 // initialises b.a.b,而不是b.b
};

但是,IMHO已经通过 -Wmissing-field -initializers ,它不会对您的原始代码发出警告。


Consider the following program:

#include <array>

int main()
{
  std::array<int, 1> x = { 0 }; // warning!
  x = { { 0 } }; // no warning
  return 0;
}

The first initialization leads to warnings on gcc 4.7.2...

main.cpp:5:22: warning: unused variable ‘x’ [-Wunused-variable]

... and clang 3.1

main.cpp:5:28: warning: suggest braces around initialization of subobject [-Wmissing-braces]
  std::array<int, 1> x = { 0 };

As far as the standard goes, there should be no difference between double or single curly braces, at least in this example.

There are two ways to deal with the warning:

  1. Just turn it off
  2. Fix the code, so the compiler is happy

What do you propose? IMHO, the double curly expression looks somewhat ugly. On the other hand, the warning might detect real problems in more complicated examples. Do you know an example where the warning would have helped you?

解决方案

-Wmissing-braces will no longer be enabled in GCC's -Wall (for C++ mode), as of 4.8, for precisely the reason you describe. For current versions of GCC, either disable or ignore the warning, the code you have is written the way it should be.

The warning is probably meant to cover code such as

struct A { int a; int b; };
struct B { A a; int b; };
B b = {
  1,
  2 // initialises b.a.b, not b.b
};

However, IMHO, that is already handled well enough by -Wmissing-field-initializers, which does not warn about your original code.

这篇关于是明智的,忽略gcc / clang的“-Wmissing-braces”警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆