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

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

False positive with -Wmaybe-uninitialized

Sometimes on GCC compilers below version 5.1 you may get an -Wmaybe-uninitialized warning when copiling 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 = std::make_optional(false, int());

This is obviously redundant, but makes the warning disappear.


PrevUpHomeNext