康桓瑋
康桓瑋

Reputation: 42861

Different declarations of the lambda that capture the parameter inside the C++20 requires expressions

Consider the following two simple concepts:

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

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

In my opinion, the two declarations should be equivalent, but GCC rejects the concept C2 and says:

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

Why GCC only accepts the concept C1, or this is just a bug? If not, what is the difference between those two declarations?

Upvotes: 3

Views: 95

Answers (1)

Daveed V.
Daveed V.

Reputation: 964

A simple-capture (like [t] in your second example) can only capture local variables and/or this. However, the parameters of a requires-expression aren’t local variables. That’s not an issue for init-captures (as in your first example).

Upvotes: 3

Related Questions