A c++20 requires expression can be used in order to check whether specific functions or types exist e.g. in a template argument and used in a concept.
In the code snipplet below I use a requires
expression in order to check the signature of a function.
I expected the requires
expression to evaluate to true
both for the lambda and for the defined function.
The expression fails for the lambda expression though.
Why that is the case?
int func(int a) noexcept { return 1; }
int main() {
auto lam = [](int a) noexcept -> int { return 1; };
// works fine for a function with this signature:
static_assert(requires(int a) { { func(a) } ->std::same_as<int>; });
// the following three conditions each evaluate to false for the lambda
static_assert(requires(int a) {
lam(a);
{lam(a)}->std::same_as<int>;
requires std::is_same_v<decltype(lam(a)), int>;
});
return 0;
}
The same applies for arbitrary member functions:
struct T { void f() noexcept; };
int main() {
static_assert(requires(T o) { o.f(); });
}
Compiled on gcc-10.0 and gcc-trunk, the code snipplet is available on compiler explorer.
This is a bug in GCC that is now fixed for the release of gcc 10.0.