【发布时间】:2021-02-07 12:19:13
【问题描述】:
我在搞乱新的 C++20 lambda,似乎我可以声明一个采用非类型模板参数的 lambda,但是我无法调用它。
#include <iostream>
int main() {
// compiles fine
auto f = []<bool ok>() { return ok; };
// it even has an address??
std::cout << &f;
// f(); // error : no matching function for call to object of typ
// f<true>(); // error : invalid operands to binary expression
f.operator()<true>(); // compiles but somewhat... ugly
}
我查看了相关论文here,但似乎没有提到这种情况下的调用语法。
是否禁止在 lambda 调用站点显式传递模板参数?这将是一个令人失望的限制,因为我认为其目的是让 lambda 能够像模板一样发挥作用。
【问题讨论】:
-
一种可能性是直接在 lambda 中调用函数模板:
f.operator()<true>();。不知道有没有别的办法。 -
@tkausl,很好,我没想到它可以工作 :)
标签: c++ templates lambda c++20