Today I found this code
#include <cstdio> auto terminal = [](auto term) { return [=] (auto func) { return terminal(func(term)); }; };
Surprisingly, GCC accepts it. Clang rejects it because it uses terminal
in its own intializer and is declared auto
.
I was expecting the error that clang gave, but is it actually ill-formed? Or must the code be accepted?
It is necessary to initialize the variable when declaring it using the auto keyword.
You can initialize any auto variable except function parameters. If you do not explicitly initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C or C++ expression.
The auto keyword is a simple way to declare a variable that has a complicated type. For example, you can use auto to declare a variable where the initialization expression involves templates, pointers to functions, or pointers to members.
I think this runs into §7.1.6.4 [dcl.spec.auto]/p11:
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.
You need the type of terminal
to determine the type of the id-expression terminal
in return terminal(func(term));
(edited, hat tip @Richard Smith), but at the point of that expression you can't deduce the type of terminal
yet.
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