Given a lambda:
auto f = [](const T& var){ return var; };
Why return type of f
is T
(not const T&
)? Where is this in the Standard?
The point is:
var
) are ignored.Quotes from the standard:
About auto:
If the placeholder is the auto type-specifier, the deduced type T' replacing T is determined using the rules for template argument deduction. Obtain P from T by replacing the occurrences of auto with either a new invented type template parameter U or, if the initialization is copy-list-initialization, with std::initializer_list. Deduce a value for U using the rules of template argument deduction from a function call ([temp.deduct.call]), where P is a function template parameter type and the corresponding argument is e. If the deduction fails, the declaration is ill-formed. Otherwise, T' is obtained by substituting the deduced U into P.
About the rule of template argument deduction from a function call:
If P is not a reference type:
- If A is a cv-qualified type, the top-level cv-qualifiers of A's type are ignored for type deduction.
About reference:
If an expression initially has the type “reference to T” ([dcl.ref], [dcl.init.ref]), the type is adjusted to T prior to any further analysis.
So for var
(i.e. A
) is const T&
, the deduced return type would be T
here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With