这个问题在这里已经有了答案:
Familiar template syntax for generic lambdas
(1 个回答)
9 个月前关闭。
假设我有一个 C++20 模板 lambda:
auto foo = []<bool arg>() {
if constexpr(arg)
...
else
...
};
但是我怎么称呼它呢?我似乎找不到语法的描述。我尝试了通常的 foo<true>();
和 template foo<true>();
,这两个 gcc 似乎都不喜欢。
最佳答案
foo.template operator()<true>();
是正确的语法。试试看 godbolt.org这种奇怪语法的原因是:
foo
是一个通用的 lambda,它实现了 template<bool> operator()
方法。 foo.operator()<true>()
会解释<
作为比较运算符。 如果您想要更易读的语法,请尝试使用
std::bool_constant
:auto foo = []<bool arg>(std::bool_constant<arg>) {
...
};
foo(std::bool_constant<true>{});
试试看 godbolt.org
关于C++20 模板 lambda : how to specify template argument?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66182453/