在 C++20 中捕获参数的 lambda 的不同声明需要表达式
Different declarations of the lambda that capture the parameter inside the C++20 requires expressions
考虑以下两个简单的概念:
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
,还是这只是一个错误?如果不是,这两个声明有什么区别?
A simple-capture(比如第二个例子中的[t]
)只能捕获局部变量and/orthis
。但是,requires-expression 的参数不是局部变量。这不是 init-captures 的问题(如您的第一个示例)。
考虑以下两个简单的概念:
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
,还是这只是一个错误?如果不是,这两个声明有什么区别?
A simple-capture(比如第二个例子中的[t]
)只能捕获局部变量and/orthis
。但是,requires-expression 的参数不是局部变量。这不是 init-captures 的问题(如您的第一个示例)。