简体   繁体   English

为什么不能将constexpr与lambda函数一起使用?

[英]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.

相关问题 为什么我不能在 constexpr lambda 函数中使用 std::tuple - Why can't I use a std::tuple in a constexpr lambda function 为什么我的constexpr函数不能返回lambda? - Why can't my constexpr function return a lambda? 为什么我不能在类的另一个函数的声明中使用静态constexpr的结果? - Why can't I use the result of a static constexpr in the declaration of another function in a class? 为什么我不能递增简单constexpr函数的参数? - Why I can't increment the parameter of the simple constexpr function? 为什么lambda在转换为函数指针时不能在constexpr上下文中使用? - Why can't lambda, when cast to function pointer, be used in constexpr context? 为什么只能通过捕获 lambda 才能看到 constexpr“变量”? - Why can't a constexpr "variable" be seen only by a capturing lambda? 为什么我不能constexpr绑定? - Why Can't I constexpr a bind? 我可以在 lambda 中使用 constexpr 值而不捕获它吗? - Can I use a constexpr value in a lambda without capturing it? 为什么我不能在函数中使用constexpr值,但我可以在此值的范围内执行相同的操作? - Why can not I use constexpr value in function, but I can do the same in scope of this value? 我可以使用C ++ 17 captureless lambda constexpr转换运算符的结果作为函数指针模板非类型参数吗? - Can I use the result of a C++17 captureless lambda constexpr conversion operator as a function pointer template non-type argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM