This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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

--- Comment #1 from Pedro Alves <palves at redhat dot com> ---
If you uncomment the lines to use std::optional instead, you get:

 $ /opt/gcc/bin/g++ optional.cc -g3 -O2 -Wall -std=gnu++17 -c 
 optional.cc: In function ‘void func()’:
 optional.cc:28:15: warning: ‘maybe_a.A::m’ may be used uninitialized in this
function [-Wmaybe-uninitialized]
    ~A () { set (m); }
            ~~~~^~~
 optional.cc:41:15: note: ‘maybe_a.A::m’ was declared here
    Optional<A> maybe_a;
                ^~~~~~~

This warns at a different location from the reproducer in the OP.

If you revert back to use the self-contained optional, and change the
optional::m_dummy field to be of empty struct type, similarly to
std::optional:

  union
  {
    struct {} m_dummy;
    T m_item;
  };

then you get the exact same warning you get with std::optional.

I suspect that the "maybe_a.A::m" location is a bug of its own and a red
herring here, but I can't be sure.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]