[Bug middle-end/127236] New: false positive -Wdangling-pointer for std::vector initializer_list in a conditional operator

chfast at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Sep 7 09:51:44 GMT 2026


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

            Bug ID: 127236
           Summary: false positive -Wdangling-pointer for std::vector
                    initializer_list in a conditional operator
           Product: gcc
           Version: 15.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chfast at gmail dot com
  Target Milestone: ---

g++ -Werror=dangling-pointer

#include <string>
#include <vector>

struct S { std::string a, b; };

std::vector<S> f(bool c)
{
    return c ? std::vector<S>{} : std::vector{S{}};
}

https://godbolt.org/z/3dfGfhrbh

<source>: In function 'std::vector<S> f(bool)':
<source>:9:1: error: dangling pointer to an unnamed temporary may be used
[-Werror=dangling-pointer=]
    9 | }
      | ^
<source>:8:50: note: unnamed temporary defined here
    8 |     return c ? std::vector<S>{} : std::vector{S{}};
      |                                                  ^

The initializer_list backing array lives to the end of the full-expression
([dcl.init.list]/5, [class.temporary]/4), and vector's initializer_list
constructor copies out of it, so no pointer into it escapes.

Reproduces on 15.1, 15.2, 15.3 and trunk; 14.4 and earlier are clean. Removing
the conditional operator, naming the S object instead of using a temporary, or
giving S only one non-trivial member each silence it.


More information about the Gcc-bugs mailing list