【问题标题】:Different declarations of the lambda that capture the parameter inside the C++20 requires expressions捕获 C++20 内部参数的 lambda 的不同声明需要表达式
【发布时间】:2020-11-29 10:26:07
【问题描述】:

考虑以下两个简单的概念:

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

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

在我看来,这两个声明应该是等价的,但是 GCC 拒绝了 concept C2 并说:

<source>:10:9: error: use of parameter outside function body before ';' token

Why GCC only accepts the concept C1,或者这只是一个错误?如果不是,这两个声明有什么区别?

【问题讨论】:

    标签: c++ lambda language-lawyer c++20 c++-concepts


    【解决方案1】:

    simple-capture(如第二个示例中的[t])只能捕获局部变量和/或this。但是,requires-expression 的参数不是局部变量。这对 init-captures 来说不是问题(就像在您的第一个示例中一样)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 2021-06-09
      相关资源
      最近更新 更多