下面的代码合法吗?
template <auto Lambda>
struct A {};
int main () {
auto lmb = [](int i){return i*i;};
A<lmb> a;
return 0;
}
我注意到 g++ 编译得很好,而 clang++ 返回
error: a non-type template parameter cannot have type '(lambda at main.cpp:...)'
.
最佳答案
Can lambdas be used as non-type template parameter?
是的,已经实现了 P0732R2 - Class types in non-type template parameters但是
clang++
尚未实现。来源:
https://en.cppreference.com/w/cpp/compiler_support
请注意,lambda 必须至少为
constexpr
(默认情况下):When this specifier is not present, the function call operator will be
constexpr
anyway, if it happens to satisfy allconstexpr
function requirements.
但是,您可以添加
constexpr
在 lambda 本身而不是将其用作模板参数时获取错误。附带说明:您也可以将其指定为 consteval
使其作为非类型模板参数工作。有状态的 lambda 可以是
constexpr
:constexpr auto lmb1 = [](int i) {
static int x = 0;
return i*i + ++x;
};
而 lambda 通过引用捕获,或通过复制捕获 和 变异(
mutable
),不能。通过复制 constexpr
捕获不过没关系。通用 lambda 表达式可能是
constexpr
也:constexpr auto gen_lmb = []<typename T>(T& val) {
val += val;
return val;
};
template <auto Lambda>
struct A {
template<typename T>
void doit(T&& arg) {
std::cout << Lambda(arg) << '\n';
}
};
//...
A<gen_lmb> ginst;
int v = 1000;
ginst.doit(v);
ginst.doit(std::string("foo "));
std::cout << v << '\n';
2000
foo foo
2000
关于c++ - lambda 可以用作非类型模板参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62324050/