Opened 8 years ago

Last modified 6 years ago

#11169 new Bugs

boost::optional triggers -Wmaybe-uninitialized errors in g++

Reported by: Mathias Stearn <redbeard0531@…> Owned by: Fernando Cacciola
Milestone: To Be Determined Component: optional
Version: Boost 1.57.0 Severity: Problem
Keywords: Cc:

Description

Example taken from StackOverflow question 21755206*

#include <boost/optional.hpp>

::boost::optional<int> getitem();

int go(int nr)
{
  boost::optional<int> a = getitem();
  boost::optional<int> b;

  if (nr > 0)
    b = nr;

  if (a != b)
    return 1;

  return 0;
}
g++ -c -O2 -Wall /tmp/test.cpp
/tmp/test.cpp: In function ‘int go(int)’
/tmp/test.cpp:13:3: warning: ‘*((void*)& b +4)’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   if (a != b)
   ^

This has been reported as gcc bug 47679*.

Two workarounds that seem to work in my testing are value-initializing dummy_ in the aligned_storage default constructor or adding an empty asm statement that lists dummy_ as an output. Would it be possible to include a fix in mainline boost?

  • Sorry, but trac flags this as spam if I include direct links.

Change History (2)

comment:1 by akrzemi1, 7 years ago

Could you give me an example of such an asm statement?

comment:2 by anonymous, 6 years ago

Note that this GCC bug is fixed in GCC 5.1. Also as per th StackOverflow response, use a workaround by initializing an optional with a 'fabricated' empty value:

boost::optional<int> b = boost::make_opitonal(false, int());
Note: See TracTickets for help on using tickets.