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

            Bug ID: 126656
           Summary: False Positive -Wdangling-reference
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: krebbel at gcc dot gnu.org
  Target Milestone: ---

struct Address {
    virtual ~Address() = default;
};

int pool[1];
int& Get(Address) { return pool[0]; }

void test() {
    const int& ref = Get({});
}

$ g++  -Wdangling-reference minimal.cpp -c

minimal.cpp: In function ‘void test()’:
minimal.cpp:9:16: warning: possibly dangling reference to a temporary
[-Wdangling-reference]
    9 |     const int& ref = Get({});
      |                ^~~
minimal.cpp:9:25: note: ‘Address’ temporary created here
    9 |     const int& ref = Get({});
      |                      ~~~^~~~

Warning disappears with:
- Removing "virtual" from the destructor
- Turning the array into a plain int
- Removing the const qualifier in test
- (Obviously) When adding [[gnu::no_dangling]] to Get or struct Address
PR109642

Reply via email to