JLM*_*JLM 6 c++ c++-concepts c++20
仅供参考:C++17 std::is_invocable_v 完全符合我的预期。
想象一个概念来检查是否可以使用特定参数类型调用可调用对象:
template <typename Fn, typename... Args>
concept has_request_interface = requires (Fn request, Args&&... args)
{
{ std::invoke(request, std::forward<Args>(args)...) }-> Status;
};
Run Code Online (Sandbox Code Playgroud)
相对
template <typename Fn, typename... Args>
concept has_request_interface = requires (Fn request, Args... args)
{
{ std::invoke(request, args...) }-> Status;
};
Run Code Online (Sandbox Code Playgroud)
在 requires 表达式中使用完美转发有意义吗?
在我看来答案是肯定的,因为请求可调用对象可能需要某些参数的右值。
但是它的requires (Fn request, Args... args)行为是否作为关于 的左值性质的函数声明args...?
它的行为将与它看起来的完全一样。这就是requires表达式的重点:使这些东西看起来像 C++。所以它的行为就像 C++。
重要的是你如何使用这个概念。也就是说,当你requires有一些基于它的模板时,你应该正确地调用这个概念。例如:
template<typename Func, typename ...Args
void constrained(Func func, Args &&...args)
requires has_request_interface<Func, Args...>
{
Status status = func(std::forward<Args>(args)...);
}
Run Code Online (Sandbox Code Playgroud)
所以是的,如果你想通过概念转发工作,你的概念需要使用&&和转发。