This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 116a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-12-19


975. Restrictions on return type deduction for lambdas

Section: 7.5.6  [expr.prim.lambda]     Status: CD3     Submitter: Jason Merrill     Date: 4 September, 2009

N3092 comment US 30

[Moved to DR status at the April, 2013 meeting as part of paper N3638.]

There does not appear to be any technical difficulty that would require the current restriction that the return type of a lambda can be deduced only if the body of the lambda consists of a single return statement. In particular, multiple return statements could be permitted if they all return the same type.

Proposed resolution (August, 2011):

Change 7.5.6 [expr.prim.lambda] paragraph 4 as follows:

...If a lambda-expression does not include a trailing-return-type, it is as if the trailing-return-type denotes the following type:

[Example:

  auto x1 = [](int i){ return i; }; // OK: return type is int
  auto x2 = []{ return { 1, 2 }; }; // error: the return type is void (a
                                    // braced-init-list is not an expression)
  struct A { int fn1(); const int& fn2(); };
  template <class T> void f () {
    [](T t, bool b){
      if (b)
        return t.fn1();
      else
        return t.fn2();
    };
  }
  template void f<A>();             // OK: lambda return type is int

end example]

Additional note (February, 2012):

EWG requested that the adoption of this proposed resolution be postponed to allow them to discuss it. The issue has thus been returned to "review" status pending EWG action.