c++ - g++-5.1.1 warns about unused variable only when optimization flag is used -


in large project, i've been getting compiler warnings g++-5.1.1 when building release version (which uses optimization flags) not while building debug version (which disables compiler optimization). i've narrowed down problem minimal example listed below commands reproduce problem. problem not occur if use g++-4.8.4. bug in g++-5.1.1? or, code doing legitimately wrong , warrants warning? why doesn't produce warnings last 3 cases listed in code (see edit @ bottom explanation)?

for interested, here bug report in gcc's bugzilla.

/*  code complains variable 'container' unused if optimization flag used g++-5.1.1 while g++-4.8.4 not produce warnings in either case.  here commands try out:  $ g++ --version g++ (gcc) 5.1.1 20150618 (red hat 5.1.1-4) copyright (c) 2015 free software foundation, inc. free software; see source copying conditions.  there no warranty; not merchantability or fitness 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 not used [-wunused-variable]  const std::array<item, 5> container {} ;                             ^ */ #include <array> struct item  {     int itemvalue_ {0} ;     item() {} ; } ;  // // warning go away if 1 of following: //  // - comment out constructor item. // - remove 'const' next line (i.e. make container non-const). // - remove '{}' next line (i.e. remove initializer list). // const std::array<item, 5> container {} ; // // these lines not produce warnings: // const std::array<item, 5> container_1 ; std::array<item, 5> container_2 ; std::array<item, 5> container_3 {} ; 

edit: mentioned ryan haining in comments, container_2 , container_3 have extern linkage , compiler has no way of warning usage.

this looks bug, if @ documentation -wunused-variable says (emphasis mine):

this option implies -wunused-const-variable c, but not c++

and if @ -wunused-const-variable says:

warn whenever constant static variable unused aside declaration. warning enabled -wunused-variable c, not c++. in c++ not error since const variables take place of #defines in c++.

and can see warning goes away in the head revision of gcc.

this gcc bug report: -wunused-variable ignores unused const initialised variables relevant. although against c c++ case discussed.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -