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

--- Comment #11 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpola...@gcc.gnu.org>:

https://gcc.gnu.org/g:80f0052b3924569af904d1bab0858fe985f33a94

commit r13-6529-g80f0052b3924569af904d1bab0858fe985f33a94
Author: Marek Polacek <pola...@redhat.com>
Date:   Tue Jan 17 17:34:58 2023 -0500

    c++: -Wdangling-reference with reference wrapper [PR107532]

    Here, -Wdangling-reference triggers where it probably shouldn't, causing
    some grief.  The code in question uses a reference wrapper with a member
    function returning a reference to a subobject of a non-temporary object:

      const Plane & meta = fm.planes().inner();

    I've tried a few approaches, e.g., checking that the member function's
    return type is the same as the type of the enclosing class (which is
    the case for member functions returning *this), but that then breaks
    Wdangling-reference4.C with std::optional<std::string>.

    This patch adjusts do_warn_dangling_reference so that we look through
    reference wrapper classes (meaning, has a reference member and a
    constructor taking the same reference type, or is std::reference_wrapper
    or std::ranges::ref_view) and don't warn for them, supposing that the
    member function returns a reference to a non-temporary object.

            PR c++/107532

    gcc/cp/ChangeLog:

            * call.cc (reference_like_class_p): New.
            (do_warn_dangling_reference): Add new bool parameter.  See through
            reference_like_class_p.

    gcc/testsuite/ChangeLog:

            * g++.dg/warn/Wdangling-reference8.C: New test.
            * g++.dg/warn/Wdangling-reference9.C: New test.

Reply via email to