问题描述
在文章 Perfect forwarding and universal references in C++ 中,我有一个不明白的时刻。 在段落用 std::forward 解决完美转发的第一种情况我理解,但第二个没有。
The other case to handle is:
wrapper(42,3.14f);
Here the arguments are rvalues,so T1 is deduced to int. We get the call func(forward<int>(e1),...). Therefore,forward is instantiated with int and we get this version of it [3]:
int&& forward(int&& t) noexcept {
return static_cast<int&&>(t);
}
包装是
template <typename T1,typename T2>
void wrapper(T1&& e1,T2&& e2) {
func(forward<T1>(e1),forward<T2>(e2));
}
因为前向作者说在这种情况下使用了这个重载
template<class T>
T&& forward(typename std::remove_reference<T>::type& t) noexcept {
return static_cast<T&&>(t);
}
所以在调用 wrapper(42,3.14f);我们有
void wrapper(int&& && e1,float&& && e2) {
func(forward<T1>(e1),forward<T2>(e2));
推导出来
void wrapper(int e1,float e2) {
func(forward<int>(e1),forward<float>(e2));
然后调用转发之一。因为它是向前的(typename std::remove_reference::type& t)我们还有
int&& forward(typename std::remove_reference<int>::type& t) noexcept
-->
int&& forward(typename int& t) noexcept
又来了
return static_cast<int& &&>(t);
将返回 int&。但是在这个例子中我们应该得到 int&& 。所以我显然不明白一些事情。对我来说,似乎 typename std::remove_reference::type& t 将始终返回引用,因此转发的返回将始终为 return static_cast<T& &&>(t);
需要帮助,请。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)