This proposal also resolves core DRs 975 (lambda return type deduction from multiple return statements), 1048 (inconsistency between auto and lambda return type deduction), and 1588 (deducing cv-qualified auto).
and if we allow it in that situation, it should be valid in other situations as well. Allowing it is also the more orthogonal choice; in general, I believe that if combining two features can work, it should work.struct A { auto f(); // forward declaration }; auto A::f() { return 42; }
The proposed wording allows non-defining declarations so long as all declarations have the same declared type, without considering the deduced type.
And similarly for templates:auto f(); // return type is unknown auto f() { return 42; } // return type is int auto f(); // redeclaration int f(); // error, declares a different function
template <class T> auto g(T t); // forward declaration template <class T> auto g(T t) { return t; } // return type is deduced at instantiation time template <class T> auto g(T t); // redeclaration
Of course, using such a function in an expression when only a forward declaration has been seen is ill-formed:
An explicit specialization or instantiation of an auto template must also use auto. An explicit specialization or instantiation of a non-auto template must not use auto.auto f(); // return type is unknown int i = f(); // error, return type of f is unknown
template <class T> auto f(T t) { return t; } // #1 template auto f(int); // OK template char f(char); // error, no matching template template<> auto f(double); // OK, forward declaration with unknown return type template <class T> T f(T t) { return t; } // OK, not functionally equivalent to #1 template char f(char); // OK, now there is a matching template template auto f(float); // OK, matches #1
The limitation on return type deduction to function bodies consisting of a single return statement is inconvenient for lambdas (DR 975), but is likely to be even more of an annoyance on normal functions.
Both return statements here return int, we can determine that perfectly well. The proposed wording allows this example, and resolves core issue 975.auto iterate(int len) // error, body isn't "return expr;" { for (int i = 0; i < len; ++i) if (search (i)) return i; return -1; }
but once we have deduced a return type, there is no reason to prohibit recursion.auto h() { return h(); } // error, return type of h is unknown
auto sum(int i) { if (i == 1) return i; // return type deduced to int else return sum(i-1)+i; // ok to call it now }
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time typedef decltype(f(1)) fint_t; // must instantiate f<int> to deduce return type
To allow this, we should use the same auto deduction rules for function and lambda return type that we do for auto variables. This would also resolve core DR 1048, which objects to the difference in handling of cv-qualifiers between auto deduction and lambda return type deduction and was classified as an extension.template <class T> struct A { static T t; }; template <class T> auto& f() { return A::t; } // returns by reference
Such a function must have a return statement, however, since there is no way to get void from auto&.
auto& f() { } // error, no return statement
Unfortunately, there is no way to get the effect of decltype with an auto return type; plain auto never deduces to a reference, and auto&& always deduces to a reference. This is a significant problem, as it means that forwarding functions can't use auto. We could consider using decltype semantics instead of the existing auto semantics, but that would mean giving different deduction semantics to auto depending on whether the declaration is of a variable or a function, and making auto functions different from lambdas.
Therefore, I propose to also allow decltype(auto) to get the decltype semantics without having to repeat the expression. For simplicity of specification and orthogonality I propose to allow it everywhere that plain auto is allowed, except for introducing a trailing-return-type. It occurs to me that the difference in meaning of decltype depending on the form of the expression (e.g. parenthesized or not) might be more surprising in this context, but I think it would be even more surprising if this worked differently from C++11 decltype.
A few people have requested that an auto function should have a deduced noexcept-specifier as well as return type. I don't think that tying the two deductions together is appropriate, but if people want general deduction of noexcept-specifiers, EWG might wish to revisit N3207.
[]()->auto& { return f(); }
Change 7.1.6.2¶4:
- decltype-specifier:
- decltype ( expression )
- decltype ( auto )
For an expression e, The type denoted by decltype(e) is defined as follows:Change 7.1.6.4:The operand of the decltype specifier is an unevaluated operand (Clause 5). [ Example:
- if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded func- tions, the program is ill-formed;
- otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;
- otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;
- otherwise, decltype(e) is the type of e.
—end example ]const int&& foo(); int i; struct A { double x; }; const A* a = new A(); decltype(foo()) x1 = i; // type is const int&& decltype(i) x2; // type is int decltype(a->x) x3; // type is double decltype((a->x)) x4 = x3; // type is const double&For decltype(auto), see 7.1.6.4.
The auto and decltype(auto) type-specifiers designate a placeholder type that will be replaced later, either by deduction from an initializer or by explicit specification withChange 5.1.2¶4:signifies that the type of a variable being declared shall be deduced from its initializer or that a function declarator shall includea trailing-return-type.The
auto type-specifier mayplaceholder type can appear with a function declarator in the decl-specifier-seq, type-specifier-seq, conversion-function-id, or trailing-return-type,with a trailing-return-type (8.3.5)in any context where such a declarator is valid. If the function declarator includes a trailing-return-type (8.3.5), that specifies the declared return type of the function. If the declared return type of the function contains a placeholder type, the return type of the function is deduced from return statements in the body of the function, if any.
Otherwise, the type of the variableThe type of a variable declared using auto or decltype(auto) is deduced from its initializer.The name of the variable being declared shall not appear in the initializer expression.This useof autois allowed when declaring variables in a block (6.3), in namespace scope (3.3.6), and in a for-init-statement (6.5.3). auto or decltype(auto) shall appear as one of the decl-specifiers in the decl-specifier-seq and the decl-specifier-seq shall be followed by one or more init-declarators, each of which shall have a non-empty initializer.[ Example:
— end example ]auto x = 5; // OK: x has type int const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int static auto y = 0.0; // OK: y has type double auto int r; // error: auto is not a storage-class-specifier auto f() -> int; // OK: f returns int auto g() { return 0.0; } // OK: g returns double auto h(); // OK, h's return type will be deduced when it is defined
The auto type-specifierA placeholder type can also be used in declaring a variable in the condition of a selection statement (6.4) or an iteration statement (6.5), in the type-specifier-seq in the new-type-id or type-id of a new-expression (5.3.4), in a for-range-declaration, and in declaring a static data member with a brace-or-equal-initializer that appears within the member-specification of a class definition (9.4.2).A program that uses auto or decltype(auto) in a context not explicitly allowed in this section is ill-formed.
Once the type of a declarator-id has been determined according to 8.3, the type of the declared variable using the declarator-idWhen a variable declared using a placeholder type is initialized, or a return statement occurs in a function declared with a return type that contains a placeholder type, the deduced return type or variable type is determined from the type of its initializer. In the case of a return with no operand, the initializer is considered to be void(). Let T be the declared type of the variable or return type of the function.If the placeholder is the auto type-specifier, the deduced type is determined using the rules for template argument deduction.
Let T be the declared type that has been determined for a variable identifier d.If the deduction is for a return statement and the initializer is a braced-init-list (8.5.4), the program is ill-formed. Otherwise, Obtain P from T by replacing the occurrences of auto with either a new invented type template parameter U or, if the initializer is a braced-init-list(8.5.4), with std::initializer_list<U>.The type deduced for the variable d is then the deduced A determinedDeduce a value for U using the rules of template argument deduction from a function call (14.8.2.1), where P is a function template parameter type and the initializerfor dis the corresponding argument. If the deduction fails, the declaration is ill-formed. Otherwise, the type deduced for the variable or return type is obtained by substituting the deduced U into P. [ Example:— end example ]auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int> auto x2 = { 1, 2.0 }; // error: cannot deduce element typeIf the placeholder is the decltype(auto) type-specifier, the declared type of the variable or return type of the function shall be the placeholder alone. The type deduced for the variable or return type is determined as described in 7.1.6.2, as though the initializer had been the operand of the decltype. [ Example:
— end example ]int i; int&& f(); auto x3a = i; // decltype(x3a) is int decltype(auto) x3d = i; // decltype(x3d) is int auto x4a = (i); // decltype(x4a) is int decltype(auto) x4d = (i); // decltype(x4d) is int& auto x5a = f(); // decltype(x5a) is int decltype(auto) x5d = f(); // decltype(x5d) is int&& auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int> decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression auto *x7a = &i; // decltype(x7a) is int* decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)If the list of declarators contains more than one declarator, the type of each declared variable is determined as described above. If a function with a declared return type that contains a placeholder type has multiple return statements, the return type is deduced for each return statement. In either case, if the type deduced
for the template parameter Uis not the same in each deduction, the program is ill-formed. [ Example:The type of i is the deduced type of the parameter u in the call f(expr) of the following invented function template:const auto &i = expr;— end example ]template <class U> void f(const U& u);If a function with a declared return type that uses a placeholder type has no return statements, the return type is deduced as though from a return statement with no operand at the closing brace of the function body. [ Example:
— end example ]auto f() { } // OK, return type is void auto* g() { } // error, cannot deduce auto* from void()If the type of an entity with an undeduced placeholder type is needed to determine the type of an expression, the program is ill-formed. But once a return statement has been seen in a function, the return type deduced from that statement can be used in the rest of the function, including in other return statements. [ Example:
—end example]auto n = n; // error, n's type is unknown auto f(); void g() { &f; } // error, f's return type is unknown auto sum(int i) { if (i == 1) return i; // sum's return type is int else return sum(i-1)+i; // OK, sum's return type has been deduced }Return type deduction for a function template with a placeholder in its declared type occurs when the definition is instantiated even if the function body contains a return statement with a non-type-dependent operand. [ Note: So any use of a specialization of the function template will cause an implicit instantiation. Any errors that arise from this instantiation are not in the immediate context of the function type, and can result in the program being ill-formed. —end note ] [ Example:
—end example]template <class T> auto f(T t) { return t; } // return type deduced at instantiation time typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type template<class T> auto f(T* t) { return *t; } void g() { int (*p)(int*) = &f; } // instantiates both 'f's to determine return types, chooses secondRedeclarations or specializations of a function or function template with a declared return type that uses a placeholder type shall also use that placeholder, not a deduced type. [ Example:
auto f(); auto f() { return 42; } // return type is int auto f(); // OK int f(); // error, cannot be overloaded with auto f() decltype(auto) f(); // error, auto and decltype(auto) don't match template <typename T> auto g(T t) { return t; } // #1 template auto g(int); // OK, return type is int template char g(char); // error, no matching template template<> auto g(double); // OK, forward declaration with unknown return type template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1 template char g(char); // OK, now there is a matching template template auto g(float); // still matches #1 void h() { return g(42); } // error, ambiguous template <typename T> struct A { friend T frf(T); }; auto frf(int i) { return i; } // not a friend of A<int>A function declared with a return type that uses a placeholder type shall not be virtual (10.3).
An explicit instantiation declaration (14.7.2) does not cause the instantiation of an entity declared using a placeholder type, but it also does not prevent that entity from being instantiated as needed to determine its type. [ Example:
—end example ]template <typename T> auto f(T t) { return t; } extern template auto f(int); // does not instantiate f<int> int (*p)(int) = f; // instantiates f<int> to determine its return type, // but an explicit instantiation definition is still required somewhere in the program
If a lambda-expression does not include a lambda-declarator, it is as if the lambda-declarator were ().Change 14.7.2¶10:If a lambda-expression does not include a trailing-return-type, it is as if the trailing-return-type denotes the following type:The lambda return type is auto, which is replaced by the trailing-return-type if provided and/or deduced from return statements as described in 7.1.6.4. [ Example:
if the compound-statement is of the form{ attribute-specifier-seqopt return expression ; }the type of the returned expression after lvalue-to-rvalue conversion (4.1), array-to-pointer conver- sion (4.2), and function-to-pointer conversion (4.3);otherwise, void.— end example ]auto x1 = [](int i){ return i; }; // OK: return type is int auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-listthe return type is void (a// braced-init-list is not an expression)int j; auto x3 = []()->auto&& { return j; }; // OK: return type is int&
Except for inline functions, declarations with types deduced from their initializer or return value (7.1.6.4), and class template specializations, explicit instantiation declarations have the effect of suppressing the implicit instantiation of the entity to which they refer.