简体   繁体   English

C ++ 14结合了通用lambda和变量模板

[英]C++14 combining generic lambdas and variable templates

I know about generic lambdas , and I know about variable templates, but, what does this do? 我了解泛型lambda ,也了解变量模板,但是,这是做什么的呢? Is it even allowed? 甚至允许吗?

template<typename T>
auto f = [](auto a, T b){ /**/ };

If it's allowed, can it be used as expected? 如果允许,可以按预期使用吗? That is, as f<type>(var_a, var_b) ? 也就是说,作为f<type>(var_a, var_b)

A variable template must be declared constexpr . 变量模板必须声明为constexpr A lambda cannot occur in a constant-expression , so the initialisation is not allowed, and its operator() is not declared constexpr , so calling it isn't allowed. Lambda不能在constant-expression中发生,因此不允许初始化,并且其operator() 未被声明为constexpr ,因此不允许对其进行调用。

In summary, this is ill-formed in the current C++14 draft. 总之,这在当前的C ++ 14草案中是错误的。

Note: curiously, even though a lambda-expression cannot occur in a constant-expression , it seems that the closure type of a lambda may have a constexpr copy/move constructor. 注意:奇怪的是,即使lambda-expression不能在constant-expression中出现 ,但似乎lambda的闭包类型可能具有constexpr copy / move构造函数。

该代码现在在C ++ 14的当前草案中是合法的,并且可以使用clang 3.5 trunk进行编译

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM