[英]C++ Requires expression for checking function signature does not work for Lambdas or member functions in gcc
A c++20 requires expression can be used in order to check whether specific functions or types exist eg in a template argument and used in a concept.可以使用 c++20 requires 表达式来检查特定的函数或类型是否存在于例如模板参数中并在概念中使用。
In the code snipplet below I use a requires
expression in order to check the signature of a function.在下面的代码片段中,我使用了一个requires
表达式来检查函数的签名。 I expected the requires
expression to evaluate to true
both for the lambda and for the defined function.我期望requires
表达式对于 lambda 和定义的函数都评估为true
。 The expression fails for the lambda expression though.尽管 lambda 表达式的表达式失败。 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 .在 gcc-10.0 和 gcc-trunk 上编译,代码片段在compiler explorer上可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.