[英]Why can't I use constexpr with lambda function?
I need an explanation for this. 我需要对此进行解释。 How does lambda differ from others function to it don't be allowed? 不允许lambda与其他功能有何不同? Is this a nonsense to be not part of C++ standard? 这不是C ++标准的一部分吗? for example, I wanted to write a "nested" function in C++ but like it isn't possible to do directly I do use lambda instead of. 例如,我想在C ++中编写一个“嵌套”函数,但就像不可能直接使用lambda一样。 This function does a very small job: take a single int parameter and multiply with some values and returns. 该函数做的工作非常简单:采用单个int参数,然后乘以一些值并返回。 This values are locals to where lambda function is defined and all them are constexpr
and know at run-time for this reason I do want to let the compiler put a const value instead of call the function. 这个值是lambda函数定义的本地值,它们都是constexpr
,因此在运行时知道,因此我想让编译器放置const值而不是调用函数。 It's very often used function (that's why I write this as function) and I don't want this beging computed at run-time. 它是非常常用的函数(这就是为什么我将其编写为函数),并且我不希望在运行时计算此beging。
It is because the standard list lambda as non constant expression : "5.19 Constant expressions" … "is a core constant expression unless it involves one of the following as a potentially evaluated subexpression" … "— a lambda-expression (5.1.2);" 这是因为标准列表将lambda作为非常量表达式:“ 5.19常量表达式”…“是核心常量表达式,除非它涉及以下之一作为可能评估的子表达式”…“-lambda-expression(5.1.2); “
That is enough for any compiler to reject constexpr function with a lambda involved. 这足以让任何编译器拒绝包含lambda的constexpr函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.