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

Viewed 84

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?

1 Answers

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).

Related