This is an archive of the discontinued LLVM Phabricator instance.

[C++14][Sema] Disallow decltype(auto) deduction for lambdas
AbandonedPublic

Authored by Rakete1111 on Sep 10 2017, 3:09 AM.

Details

Reviewers
rsmith
Summary

decltype(auto) lambda = []{}; is currently allowed by clang, gcc and MSVC, but it is actually not valid, because decltype([]{}) (which is how the type of lambda is deduced) is ill-formed, I think. I honestly could argue both ways (and I did once), but the fact that decltype(auto) list = {0,1}; is already rejected by all three, it seems like the most sensible way is to disallow it for lambdas too.

Diff Detail

Event Timeline

Rakete1111 created this revision.Sep 10 2017, 3:09 AM
rsmith edited edge metadata.Sep 10 2017, 1:44 PM

decltype(auto) lambda = []{}; and decltype(auto) list = {0,1}; seem like very different situations to me; in the latter case, the initializer is not even an expression.

The rule that bans decltype([]{}) is that a *lambda-expression* shall not appear in an unevaluated operand. But the initializer in decltype(auto) lambda = []{}; is not an unevaluated operand. The relevant rule is [dcl.type.auto.deduct]p5, which says: "The type deduced for T is determined as described in 10.1.7.2, as though e had been the operand of the decltype." -- so, while this is certainly not completely clear, this suggests that we only consider the expression as the operand of decltype for the purpose of determining the type, not for the purpose of determining whether the deduction is valid. And certainly seems to be the interpretation that is more friendly to users.

Rakete1111 abandoned this revision.Sep 11 2017, 9:37 AM

Agreed. Closing this.