gpt4 book ai didi

c++ - lambda 函数/表达式是否支持 constexpr?

转载 作者:太空狗 更新时间:2023-10-29 23:14:49 25 4
gpt4 key购买 nike

struct Test
{
static const int value = []() -> int { return 0; } ();
};

在 gcc-4.6 中,我得到类似这样的信息,error: function needs to be constexpr。我尝试了将 constexpr 放在不同位置的多种组合,但没有成功。

constexpr 是否也支持 lambda 函数(无论是否指定了 return 类型)?正确的语法是什么?

有什么解决办法吗?

最佳答案

更新:从 C++17 开始,常量表达式中允许使用 lambda。


根据 [expr.const]/(2.6),Lambda 目前 (C++14) 不允许出现在常量表达式中,但它们一次会出现 N4487被接受(可以在工作草案 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 be constexpr if it would satisfy the requirements of a constexpr function (similar to the constexpr inference that already occurs for implicitly defined constructors and the assignment operator functions).

关于c++ - lambda 函数/表达式是否支持 constexpr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31860930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com