[Bug c++/80635] std::optional and bogus -Wmaybe-uninitialized warning

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Oct 14 22:53:00 GMT 2019


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635

--- Comment #24 from Jonathan Wakely <redi at gcc dot gnu.org> ---
From Bug 92092:

The program below gets the following warning message. I think the program is
well-formed (Clang 9.0.0 accepts it without warning).

** Compiler Flags **

-O2 -std=c++17 -Wall 

** Version **

gcc 9.2.0, tested online with Compiler Explorer ( https://gcc.godbolt.org/ )
but the warning happens on my Ubuntu machine as well (that version is gcc
8.3.0)

** Warning **

source>: In static member function 'static _Res
std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const
std::_Any_data&, _ArgTypes&& ...) [with _Res = std::optional<Color>; _Functor =
main()::<lambda()>; _ArgTypes = {}]':

<source>:13:33: warning: '<anonymous>' may be used uninitialized in this
function [-Wmaybe-uninitialized]

   13 |     return std::optional<Color>();


** Source code **

#include <functional>
#include <optional>

enum class Color { Red, Green, Blue };
size_t load(size_t);

int main() {
  size_t currentValue = load(0);
  auto ready = [currentValue]() -> std::optional<Color> {
    if (load(1) != currentValue) {
      return Color::Red;
    }
    return std::optional<Color>();
  };
  std::function<std::optional<Color>()> temp(ready);
  (void)temp;
}


More information about the Gcc-bugs mailing list