3 ms·
Can someone familiar with modern C++ explain the type mechanics of std::visit on line 14? In particular, it seems the “auto” parameter of the lambda expression
by twoodfin 8y ago
Can someone familiar with modern C++ explain the type mechanics of std::visit on line 14? In particular, it seems the “auto” parameter of the lambda expression could stand in for either type in the variant, dependent on its runtime value, but I have always thought of “auto” as a compile-time mechanism.
Is the “auto” in this case being filled in by some compiler-generated proxy type that can dispatch any common operation available across all variants?
- KayEss 8y agoThe auto there works like a templated function would (except it's anonymous as you'd expect from a lambda).
- twoodfin 8y agoI’m still confused. Presumably you can’t pass an uninstantiated template to a function, so what’s the type of the lambda expression? It’s effectively a callable object with multiple overloads but I didn’t know lambda syntax could create something like that.
- KayEss 8y agoLambdas have an un-utterable type, so the addition of a template like mechanism in their arguments is something the compiler can do behind the scenes and it all just works. Look for "generic" in this page <https://en.cppreference.com/w/cpp/language/lambda> https://en.cppreference.com/w/cpp/language/lambda>