Nov 27, 2014

[C++11] something about auto... in generic lambda

Generic lambda inconsistency? C++11's six dots
void f(int...);
// equivalent to
void f(int, ...);
Likewise..
[](auto&&...) { return 42; };
// equivalent to
[](auto&& , ...) { return 42; };
This is what allows the funny quirk of 6 periods:
template<typename... Args>
void f(Args&&......) { }
// equivalent to
template<typename... Args>
void f(Args&&..., ...) { }
So, what's the workaround? easy:
[](auto&&... t) { return 42; };

Reference:
https://en.cppreference.com/w/cpp/language/variadic_arguments
In the C programming language, at least one named parameter must appear before the ellipsis parameter, so printz(...); is not valid.
In C++, this form is allowed even though the arguments passed to such function are not accessible, and is commonly used as the fallback overload in SFINAE, exploiting the lowest priority of the ellipsis conversion in overload resolution.

This syntax for variadic arguments was introduced in 1983 C++ without the comma before the ellipsis.

When C89 adopted function prototypes from C++, it replaced the syntax with one requiring the comma.

For compatibility, C++98 accepts both C++-style f(int n...) and C-style f(int n, ...)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.