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

            Bug ID: 103543
           Summary: Potential compiler warning for return of temporary?
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at hazlewoods dot net
  Target Milestone: ---

A return of a temporary from a lambda assigned to `std::function<>` does not
cause a warning, unlike a regular function.
Would be useful if it trapped, but it may be too far down the deduction path.

<code>
#include <iostream>
#include <functional>

struct S
{
  int s{};
};

using F = std::function<S const& ()>;

S const& fn()
{
  return S{};  // Warning: returning ref to temporary
}

int main()
{
  F f = [](){ return S{}; };  // No warning
  std::cout << f().s << std::endl;  // Ouch
}
</code>


g++-11 -W -Wall -std=c++20 -O2 -pedantic-errors test.cpp --save-temps

g++-11 --version
g++-11 (GCC) 11.2.1 20211105

Reply via email to