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

            Bug ID: 116090
           Summary: [11 regression] Another -Wmaybe-uninitialized false
                    positive with std::optional
           Product: gcc
           Version: 14.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: iamsupermouse at mail dot ru
  Target Milestone: ---

I'm not sure if it's an exact duplicate, since there are many bugs about
-Wmaybe-uninitialized false positives.

GCC warns about following code when compiled with `-std=c++17
-Werror=maybe-uninitialized -O2` (tested on GCC 11/12/13/14/trunk)

https://gcc.godbolt.org/z/j44nbchza

    #include <cstddef>
    #include <optional>
    #include <vector>

    int main()
    {
        size_t n = 0;
        std::optional<float> opt = 0.0f;

        extern std::vector<int> vec;
        for (int elem : vec)
        {
            (void)elem;

            n += 1;

            bool Condition(void);
            if (Condition())
                *opt += 1;
            else
                opt.reset();
        }

        if (n && opt)
        {
            void UseValue(float);
            UseValue(*opt);
        }
    }

Error message:

    <source>: In function 'int main()':
    <source>:27:17: error: '*(float*)((char*)&opt +
offsetof(std::optional<float>,std::optional<float>::<unnamed>.std::_Optional_base<float,
true, true>::<unnamed>))' may be used uninitialized
[-Werror=maybe-uninitialized]
       27 |         UseValue(*opt);
          |         ~~~~~~~~^~~~~~
    <source>:8:26: note: '*(float*)((char*)&opt +
offsetof(std::optional<float>,std::optional<float>::<unnamed>.std::_Optional_base<float,
true, true>::<unnamed>))' was declared here
        8 |     std::optional<float> opt = 0.0f;
          |                          ^~~

Reply via email to