【发布时间】:2021-01-18 11:15:29
【问题描述】:
我想再次确认我对前向重载的工作原理有正确的理解
template< class T >
constexpr T&& forward( std::remove_reference_t<T>& t ) noexcept;
template< class T >
constexpr T&& forward( std::remove_reference_t<T>&& t ) noexcept;
如果我们用一些局部变量n 调用转发,比如int,重载将被指定为
constexpr int&& forward( int& t ) noexcept;
constexpr int&& forward( int&& t ) noexcept;
所以我们的案例将选择第一个。如果我们只使用4 调用转发,将指定相同的重载,但将选择第二个版本。所以第一次重载总是捕获所有左值,其次是所有右值。对吗?
【问题讨论】:
-
你的假设是正确的。我推荐使用cppinsights.io。
标签: c++ c++11 templates c++14 forward