constexpr是否支持lambda函数/表达式? [英] Is constexpr supported with lambda functions / expressions?
问题描述
struct Test
{
static const int value = []() - > int {return 0; }();
};
使用gcc-4.6,我得到类似于错误:function需要constexpr
。我尝试了多种组合在 constexpr
在各个地方,但没有运气。
是 constexpr
也支持lambda函数(不管 return
是否指定类型)?
> Lambdas目前(C ++ 14)不允许在[expr.const] /(2.6)的常量表达式中,但他们会一次 N4487 (可在工作草案N4582中找到):
此建议建议在常量
表达式中允许使用lambda表达式,删除现有的限制。作者建议
确定某个λ-expression s和对某些闭包
对象的操作允许出现在常量表达式中。在这样做时,
我们还建议,如果
每个数据成员的类型是字面类型,则将闭包类型视为文字类型;并且如果
在lambda声明符中省略了constexpr
说明符,那么
生成的函数调用操作符constexpr
如果它满足
需求的一个constexpr
函数(类似于
constexpr
对隐式定义的
构造函数和赋值操作符函数已经发生的推断)。
struct Test
{
static const int value = []() -> int { return 0; } ();
};
With gcc-4.6 I get something like, error: function needs to be constexpr
. I have tried multiple combinations of putting constexpr
at various places, but no luck.
Is constexpr
supported for lambda functions as well (irrespective of return
type specified or not) ? What is the correct syntax ?
Any work around possible ?
Lambdas are currently (C++14) not allowed in constant expressions as per [expr.const]/(2.6), but they will once N4487 is accepted (which can be found in the working draft N4582):
This proposal suggests allowing lambda-expressions in constant expressions, removing an existing restriction. The authors propose that certain lambda-expressions and operations on certain closure objects be allowed to appear within constant expressions. In doing so, we also propose that a closure type be considered a literal type if the type of each of its data-members is a literal type; and, that if the
constexpr
specifier is omitted within the lambda-declarator, that the generated function call operator beconstexpr
if it would satisfy the requirements of aconstexpr
function (similar to theconstexpr
inference that already occurs for implicitly defined constructors and the assignment operator functions).
这篇关于constexpr是否支持lambda函数/表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!