[英]Why does std::move() not work without _Remove_reference?
_Remove_reference exists for, as you know, converting T& to T or T&& to T. 如你所知,_Remove_reference存在,将T&转换为T或T &&转换为T.
I made the following code in a playful mood, it doesn't work at all as I expected, but have no idea why. 我以一种俏皮的心情制作了以下代码,它完全不像我预期的那样工作,但不知道为什么。
template<class _Ty>
struct _Remove_reference
{ // remove reference
typedef _Ty _Type;
static void func(){ cout << "1" << endl; }
};
// template<class _Ty>
// struct _Remove_reference<_Ty&>
// { // remove reference
// typedef _Ty _Type;
// static void func(){ cout << "2" << endl; }
// };
//
// template<class _Ty>
// struct _Remove_reference<_Ty&&>
// { // remove rvalue reference
// typedef _Ty _Type;
// static void func(){ cout << "3" << endl; }
// };
template<class _Ty> inline
typename _Remove_reference<_Ty>::_Type&&
move(_Ty&& _Arg)
{ // forward _Arg as movable
typename _Remove_reference<_Ty>::func();
return ((typename _Remove_reference<_Ty>::_Type&&)_Arg);
}
int main(){
int a1= 3;
int&& a2 = move(a1); // can't convert the a1 to int&&
return 0;
}
I guess it's all about reference collapsing rules, and template argument deduction but I am confused. 我想这都是关于参考折叠规则和模板参数推导但我很困惑。 my curiosity about this has to be shattered for me to sleep tight. 我对此的好奇心必须让我睡不着觉。
Thanks in advance. 提前致谢。
Given 特定
template<class _Ty> inline
typename _Remove_reference<_Ty>::_Type&&
move(_Ty&& _Arg)
When you do move(a1)
, _Ty
is deduced as int&
. 当你move(a1)
, _Ty
被推断为int&
。 _Ty&&
would thus still be int&
due to the reference collapsing rules, so you need to remove the reference to get int
before you can make it an rvalue reference. _Ty&&
因此仍然是int&
由于引用折叠规则,所以你需要删除引用get int
才能使它成为rvalue引用。
This is a special case of the template argument deduction rules. 这是模板参数推导规则的特例。 If you have a function argument which is T&&
where T
is a template parameter, then if you pass an lvalue (ie a named object or an lvalue reference) to the function then T
is deduced as X&
where X
is the type of the lvalue object. 如果你有一个函数参数是T&&
,其中T
是一个模板参数,那么如果你将一个左值(即一个命名对象或一个左值引用)传递给函数,那么T
推导为X&
其中X
是左值对象的类型。 If you pass an rvalue (a temporary or an rvalue reference) to the function then T
is just deduced as X
. 如果将rvalue(临时或右值引用)传递给函数,则T
仅推导为X
eg move(a1)
deduces _Ty
to be int&
, whereas move(42)
deduces _Ty
to be int
. 例如, move(a1)
推导_Ty
为int&
,而move(42)
推导_Ty
为int
。
BTW: I guess you took this code from your compiler's standard library --- names decorated with a leading underscore and a capital letter _Like _This are reserved for the compiler and standard library implementation. 顺便说一句:我猜你从编译器的标准库中获取了这个代码 - 用前导下划线和大写字母装饰的名称_Like _这是为编译器和标准库实现保留的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.