我想了解演绎指南如何与通用参考一起使用std::forward
,特别是创建完美的转发包装器。下面的代码提供了在两种情况下试验函子包装器的代码:一种具有隐式推导指南,一种具有显式推导指南。
我已经放了很多&&
and std::forward
在评论中,因为我不知道需要在哪里才能实现完美转发。我想知道哪里需要它们,哪里不需要它们。
// Case with not conversion constructor
template <class F>
struct functor1
{
explicit constexpr functor1(F/*&&*/ f)
noexcept(std::is_nothrow_copy_constructible_v<F/*&&*/>)
: _f(/*std::forward<F>(*/f/*)*/)
{}
template <class... Args>
constexpr operator()(Args&&... args)
noexcept(std::is_nothrow_invocable_v<F/*&&*/, Args/*&&*/...>)
{
/*std::forward<F>(*/_f/*)*/(std::forward<Args>(args)...);
}
private: F/*&&*/ _f;
};
// Case with a conversion constructor
template <class F>
struct functor2
{
template <class G>
explicit constexpr functor2(G&& g)
noexcept(std::is_nothrow_constructible_v<G/*&&*/, F/*&&*/>)
: _f(/*std::forward<G>(*/g/*)*/)
{}
template <class... Args>
constexpr operator()(Args&&... args)
noexcept(std::is_nothrow_invocable_v<F/*&&*/, Args/*&&*/...>)
{
/*std::forward<F>(*/_f/*)*/(std::forward<Args>(args)...);
}
private: F/*&&*/ _f;
};
template <class G>
functor2(G&&) -> functor2<G/*&&*/>;
编辑:为了简单起见,并且因为这不是问题的重点,在前面的示例中,我们认为F
and G
是函数对象,即带有operator()
.
C++ 标准定义了术语转发参考。我想通用参考用作该术语的同义词。[临时扣除呼叫]/3 https://timsong-cpp.github.io/cppwp/n4659/temp.deduct.call#3
A 转发参考是对 cv 无限定模板参数的右值引用,该参数不表示类模板的模板参数。
此概念仅适用于模板函数参数或模板构造函数参数。在所有其他情况下,T&&
is a 右值引用。的概念转发参考仅对模板参数推导有用。让我们考虑一下在下面的示例中,所有函数和构造函数都是通过int
参数(独立于其常量和值类别(左值/右值):
//possibilities of argument deduction, [cv] means any combination of "const" and "volatile":
// <"","const","volatile","const volatile">
template<class T> void f(T&);
//4 possibilities: void f([cv] int&);
template<class T> void f(const T&);
//2 possibilities: void f(const int&);
//void f(const volatile int&);
template<class T> void f(T&&);
//Forwarding reference, 8 possibilities
//void f([cv] int&);
//void f([cv] int&&);
template<class T> void f(const T&&);
//NOT a forwarding reference because of the const qualifier, 2 possibilities:
//void f(const int&&);
//void f(const volatile int&&);
template<class T>
struct S{
template<class U>
S(U&&);
//Forwarding reference, 8 posibilities:
//void S<X>([cv] int&);
//void S<X>([cv] int&&);
//no template argument deduction posible
S(T&&);
//NOT a forwarding reference, 1 possibility:
//void S<X>(X&&);
//Generated argument deduction:
//template<class T> S(T&&) -> S<T>;
//not a forwarding reference because T is a parameter of the template class;
//=> 4 possibilities: -> S<[cv] int&&>
T&& a; //an rvalue reference if T is [cv] int or [cv] int&&,
//an lvalue reference if T is [cv] int&;
//This comes from reference colapsing rules: &+&=&; &&+&=&; &&+&&=&& //(Nota: You may consider that a rvalue reference data member is probably a mistake)
};
template<class U>
S(U&&) -> S<U&&>;
//Forwarding reference, 8 possibilities:
// S<[cv] int&>;
// S<[cv] int&&>;
Using std::forward
仅在函数或构造函数体内有意义,如果std::forward
可以是右值引用或左值引用,具体取决于模板参数推导和引用折叠规则。如果std::forward
的参数总是产生右值引用,std::move
是首选,如果它总是导致左值引用,则没有任何内容是首选。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)