FYI: 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;
};对比
template <typename Fn, typename... Args>
concept has_request_interface = requires (Fn request, Args... args)
{
{ std::invoke(request, args...) }-> Status;
};在requires表达式中使用完美转发是否有意义?
在我看来,答案是肯定的,因为请求可调用对象可能期望某些参数的rvalue。
但是,requires (Fn request, Args... args)是否表现为关于args...的lvalue性质的函数声明?
发布于 2020-01-20 18:24:36
它的行为会和它的样子完全一样。这就是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)...);
}因此,如果您希望通过该概念进行转发,则您的概念需要使用&&和转发。
https://stackoverflow.com/questions/59828801
复制相似问题