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

palves at redhat dot com gcc-bugzilla@gcc.gnu.org
Thu May 4 22:31:00 GMT 2017


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

            Bug ID: 80635
           Summary: std::optional and bogus -Wmaybe-uninitilized
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: palves at redhat dot com
  Target Milestone: ---

GDB ran into an odd -Wmaybe-uninitialized warning in code using std::optional
Well, actually with gdb::optional, but std::optional shows the exact same
warning.  <Ref: https://sourceware.org/ml/gdb-patches/2017-05/msg00118.html>.

The reproducer below is a reduced self-contained testcase that triggers the
same warning.  I wasn't able to reduce it further.

 $ cat optional.cc
 //#include <optional>
 #include <new>

 template<typename T>
 struct optional
 {
   optional () : m_dummy () {}
   ~optional () { m_item.~T (); }
   void emplace () { new (&m_item) T (); }

   union
   {
     int m_dummy;
     T m_item;
   };
 };

 template <typename T>
 using Optional = optional<T>; // warns
 //using Optional = std::optional<T>; // warns too

 extern int get ();
 extern void set (int);

 struct A
 {
   A () : m (get ()) {}
   ~A () { set (m); }

   int m;
 };

 struct B
 {
   B ();
   ~B ();
 };

 void func ()
 {
   Optional<A> maybe_a;
   Optional<B> maybe_b;

   maybe_a.emplace ();
   maybe_b.emplace ();
 }

With g++ 8.0.0 20170428 pristine built from trunk:

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


More information about the Gcc-bugs mailing list