Bug 67858 - g++-5.1.1 warns about unused variable only when optimization flag is used
Summary: g++-5.1.1 warns about unused variable only when optimization flag is used
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL: http://stackoverflow.com/questions/32...
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2015-10-05 22:54 UTC by crayzeewulf
Modified: 2017-08-25 13:54 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.4, 8.0
Known to fail: 5.1.1, 5.2.0
Last reconfirmed:


Attachments
Code that demonstrates the bug. (661 bytes, text/x-csrc)
2015-10-05 22:54 UTC, crayzeewulf
Details

Note You need to log in before you can comment on or make changes to this bug.
Description crayzeewulf 2015-10-05 22:54:06 UTC
Created attachment 36446 [details]
Code that demonstrates the bug.

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 attached below with commands to reproduce the problem. The problem does not occur if I use g++-4.8.4. 

This is also discussed on StackOverflow at: http://stackoverflow.com/questions/32917767/g-5-1-1-warns-about-unused-variable-only-when-optimization-flag-is-used
Comment 1 crayzeewulf 2015-10-05 23:08:07 UTC
Here is the compiler command line and the warning:

$ g++ -Wall -std=c++11 -O3 -c bug_demo_code.cpp 
bug_demo_code.cpp:34:27: warning: ‘container’ defined but not used [-Wunused-variable]
 const std::array<Item, 5> container {} ;
Comment 2 Eric Gallager 2017-08-25 13:54:16 UTC
I get no warnings with any of the optimization levels I tried, and I tried all of them.