c++ - 未计算上下文中的 Lambda(需要表达式)

标签 c++ language-lawyer c++20

我知道关于这个主题有几个问题,但请听我说。
我知道我们可以在未评估的上下文中使用无捕获的 lambdas(如 decltype ),但是捕获的 lambdas 呢?
我在当前的 C++ 标准中找不到任何表明自 C++20 以来这是一个问题的任何内容,但我绝不是语言律师。
但我看到的是,主要编译器在 their results 中有所不同。 .
这些概念有效吗?

template <typename T>
concept C1 = requires { []{}; };

template <typename T>
concept C2 = requires(T t) { [&t]{}; };

最佳答案

I'm aware that we can use capture-less lambdas in unevaluated contexts


这不仅限于无状态的 lambda。 P0315R4 (未评估上下文中 lambdas 的措辞)删除了 lambdas(不仅仅是无状态的)不会出现在未评估 lambdas 中的限制,同时修改了与限制的原始意图相关的某些部分的措辞:

The core language changes introduced in this paper are a bit tricky. The reason is that we remove many restrictions on lambda expressions, yet we still want to keep closure types out of the signature of external functions, which would be a nightmare for implementations.


该论文没有对 lambdas w.r.t. 引入任何特殊限制。概念,我们可以简单地转向 [temp.concept]/6管理此案:

The constraint-expression of a concept-definition is an unevaluated operand ([expr.context]).


由于对 labas(无状态或无状态)不再作为操作数出现在 unevaluated contexts 中不再有任何一般限制。 ,而且在概念的上下文中没有明确的限制,这两个概念C1C2格式良好。
事实上,与某些声明中对闭包类型的限制相比,概念并未实例化,如 [temp.concept]/5 ,并且确实属于在 P0315R4 中被认为“有点棘手”的 ODR 考虑因素。

关于c++ - 未计算上下文中的 Lambda(需要表达式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68033625/

相关文章:

java - 使用 C++ 编译和运行 java 文件时无法加载主类

c++ - 如何解决 "Didn' t 在路径 : DexPathList in my native callback to Java 上找到类 "..."

c++ - cin 在读取错误类型时会覆盖我的初始化值吗?

c++ - 使用概念来选择类模板特化

c++ - 非类型模板参数可以是 "void*"类型吗?

c++ - 为什么这个 C++ 程序在 Windows 上比在 Linux 上慢?

java - 不同编程语言之间的桥梁

c - 在 glibc 中 realloc(p, 0) 真的涉及 free(p) 吗?

c - 一维访问多维数组 : is it well-defined behaviour?

c++ - ThreadSanitizer:使用 std::jthread 互斥锁的双重锁定