Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

False positive with -Wmaybe-uninitialized

Sometimes on GCC compilers below version 5.1 you may get an -Wmaybe-uninitialized warning when compiling with option -02 on a perfectly valid boost::optional usage. For instance in this program:

#include <boost/optional.hpp>

boost::optional<int> getitem();

int main(int argc, const char *[])
{
  boost::optional<int> a = getitem();
  boost::optional<int> b;

  if (argc > 0)
    b = argc;

  if (a != b)
    return 1;

  return 0;
}

This is a bug in the compiler. As a workaround (provided in this Stack Overflow question) use the following way of initializing an optional containing no value:

boost::optional<int> b = boost::make_optional(false, int());

This is obviously redundant, but makes the warning disappear.


PrevUpHomeNext