C++ code with auto
as a part of lambda argument type is accepted by GCC, e.g.:
#include <vector>
#include <iostream>
int main()
{
auto make_vector = []( std::initializer_list<auto> v )
{ return std::vector<typename decltype(v)::value_type>{v}; };
auto v = make_vector( {1,2} );
std::cout << v[0] << ' ' << v[1] << '\n';
}
https://gcc.godbolt.org/z/z43bEjMc9
Here the argument type is std::initializer_list<auto>
and it is really convenient sometimes. But other compilers complain:
A template-argument cannot be a type that contains 'auto'
Is it a GCC-only extension of the language or a new C++ feature that will appear in other compilers as well? If the first is the case then how can it be turned off to maximize language conformance?
C++14 allows lambda function (Generic lambda function) parameters to be declared with the auto.
The type of a lambda expression is unspecified. But they are generally mere syntactic sugar for functors. A lambda is translated directly into a functor.
It looks like it's a bug in GCC. Altough it says it's solved in the version of GCC you're using...
You can read here more on why you can't use auto
in this context.
It was correctly pointed out to me that this case is for a function parameter. It's still not allowed in C++20, see this answer.
This is not the valid way to declare generic lambdas. [dcl.spec.auto.general]/2:
A placeholder-type-specifier of the form type-constraintopt
auto
can be used as a decl-specifier of the decl-specifier-seq of a parameter-declaration of a function declaration or lambda-expression and, if it is not theauto
type-specifier introducing a trailing-return-type (see below), is a generic parameter type placeholder of the function declaration or lambda-expression.
Note that std::initializer_list<auto>
is not in the form of type-constraintopt auto
.
Since C++20 you can delcare template parameter list for generic lambdas. E.g.
auto make_vector = []<typename T>( std::initializer_list<T> v )
{ return std::vector<typename decltype(v)::value_type>{v}; };
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