[Bug c++/102084] Segfault when lambda is missing return type

pinskia at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Aug 26 19:46:52 GMT 2021


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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Your code is the same as this code:
template <typename T>
std::function<const T&()> constant_ref_broken(const T& c) {
  return [c]() noexcept -> const T { return c; };
}

So you are returning a non-reference and then have a return of a refence.
Nothing shocking.

If you want to capture the reference of c and return the reference rather than
what points to the refence you need c++14 to do that and this code works and
does what you originally wanted:
template <typename T>
std::function<const T&()> constant_ref_broken(const T& c) {
  return [&c]() noexcept -> decltype(auto) { return c; };
}


More information about the Gcc-bugs mailing list