In a large project, I've been getting some compiler warnings from g++-5.1.1 only when building the release version (which uses optimization flags) but not while building the debug version (which disables most compiler optimization). I've narrowed down the problem to a minimal example listed below with commands to reproduce the problem. The problem does not occur if I use g++-4.8.4. Is this a bug in g++-5.1.1? Or, is this code doing something that is legitimately wrong and warrants that warning? Why doesn't it produce any warnings for the last three cases listed in the code (see edit at the bottom for some explanation)?
在一个大型项目中,我只在构建版本版本(使用优化标志)时才从g++-5.1.1得到一些编译器警告,而在构建调试版本(这将禁用大多数编译器优化)时没有得到警告。我已经将问题缩小到下面列出的最小示例,并使用命令重新生成问题。如果我使用g+ -4.8.4,问题就不会出现。这是g++-5.1.1中的错误吗?或者,这段代码是否做了一些合理的错误并且保证了这个警告?为什么它不为代码中列出的最后三种情况产生任何警告?
For those who are interested, here is the bug report in GCC's Bugzilla.
对于感兴趣的人,这里是GCC的Bugzilla中的bug报告。
/*
This code complains that the variable 'container' is unused only if optimization
flag is used with g++-5.1.1 while g++-4.8.4 does not produce any warnings in
either case. Here are the commands to try it out:
$ g++ --version
g++ (GCC) 5.1.1 20150618 (Red Hat 5.1.1-4)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -c -std=c++11 -Wall -g -O0 test_warnings.cpp
$ g++ -c -std=c++11 -Wall -O3 test_warnings.cpp
test_warnings.cpp:34:27: warning: ‘container’ defined but not used [-Wunused-variable]
const std::array<Item, 5> container {} ;
^
*/
#include <array>
struct Item
{
int itemValue_ {0} ;
Item() {} ;
} ;
//
// The warning will go away if you do any one of the following:
//
// - Comment out the constructor for Item.
// - Remove 'const' from the next line (i.e. make container non-const).
// - Remove '{}' from the next line (i.e. remove initializer list).
//
const std::array<Item, 5> container {} ;
//
// These lines do not produce any warnings:
//
const std::array<Item, 5> container_1 ;
std::array<Item, 5> container_2 ;
std::array<Item, 5> container_3 {} ;
Edit: As mentioned by Ryan Haining in the comments, container_2
and container_3
will have extern
linkage and the compiler has no way of warning about their usage.
编辑:正如Ryan Haining在评论中提到的,container_2和container_3将会有外部链接,而编译器对它们的使用没有任何警告。
1 个解决方案
#1
1
This looks like a bug, if we look at the documentation for -Wunused-variable it says (emphasis mine):
这看起来像一个错误,如果我们看一下它说的- wunuse -variable的文档(重点是我的):
This option implies -Wunused-const-variable for C, but not for C++
这个选项意味着-Wunused-const-variable为C,但不是为c++。
and if we look at -Wunused-const-variable
it says:
如果我们看- wunuse -const-variable它说
Warn whenever a constant static variable is unused aside from its declaration. This warning is enabled by -Wunused-variable for C, but not for C++. In C++ this is normally not an error since const variables take the place of #defines in C++.
当一个常量静态变量除了声明之外没有使用时发出警告。这个警告是由C的- wunused变量启用的,但是c++没有。在c++中,这通常不是错误,因为const变量取代了c++中的#define。
and we can see this warning goes away in the head revision of gcc.
我们可以看到这个警告在gcc的头修正中消失了。
This gcc bug report: -Wunused-variable ignores unused const initialised variables is also relevant. Although it is against C the C++ case is also discussed.
这个gcc bug报告:- wunuse -variable忽略未使用的const初始化变量也是相关的。虽然它是针对C的,但是也讨论了c++的情况。
#1
1
This looks like a bug, if we look at the documentation for -Wunused-variable it says (emphasis mine):
这看起来像一个错误,如果我们看一下它说的- wunuse -variable的文档(重点是我的):
This option implies -Wunused-const-variable for C, but not for C++
这个选项意味着-Wunused-const-variable为C,但不是为c++。
and if we look at -Wunused-const-variable
it says:
如果我们看- wunuse -const-variable它说
Warn whenever a constant static variable is unused aside from its declaration. This warning is enabled by -Wunused-variable for C, but not for C++. In C++ this is normally not an error since const variables take the place of #defines in C++.
当一个常量静态变量除了声明之外没有使用时发出警告。这个警告是由C的- wunused变量启用的,但是c++没有。在c++中,这通常不是错误,因为const变量取代了c++中的#define。
and we can see this warning goes away in the head revision of gcc.
我们可以看到这个警告在gcc的头修正中消失了。
This gcc bug report: -Wunused-variable ignores unused const initialised variables is also relevant. Although it is against C the C++ case is also discussed.
这个gcc bug报告:- wunuse -variable忽略未使用的const初始化变量也是相关的。虽然它是针对C的,但是也讨论了c++的情况。