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

            Bug ID: 117509
           Summary: False negative on -Wdangling-reference
           Product: gcc
           Version: 14.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

Here's another Optional-related example:

template <class T>
struct Optional {
    union { T v; };
    bool flag;

    [[nodiscard]] auto value() && -> T&& { return (T&&)v; }
    [[nodiscard]] auto operator*() && -> T&& { return (T&&)v; }
};

auto f() -> Optional<int>;

auto g() {
    [[maybe_unused]] auto&& a = f().value(); // warns here
    [[maybe_unused]] auto&& b = *f();        // no warning here
}

value() and operator*() are synonymous for the purpose of this issue - they're
both returning an rvalue reference to v. Both of them dangle here. 

gcc gives me a warning on 'a', but not on 'b'. I tried various incantations of
[[gnu::nodangling]] to annotate operator*() to try to convince gcc that it does
actually dangle here, but to no avail.

Reply via email to