Let's say I have a C++20 template lambda:
auto foo = []<bool arg>() {
if constexpr(arg)
...
else
...
};
But how do I call it? I can't seem to find a description of the syntax. I tried the usual foo<true>();
and template foo<true>();
, neither of which gcc seems to like.
foo.template operator()<true>();
is the correct syntax. Try it on godbolt.org
The reason for this strange syntax is because:
foo
is a generic lambda which implements a template<bool> operator()
method.foo.operator()<true>()
would interpret <
as a comparison operator.If you want a slightly more readable syntax, try using a std::bool_constant
:
auto foo = []<bool arg>(std::bool_constant<arg>) {
...
};
foo(std::bool_constant<true>{});
Try it on godbolt.org
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