在C++ 03中,表达式是rvalue或lvalue.
在C++ 11中,表达式可以是:
两类已成为五大类.
我不太了解这个std::move功能
template <class T>
typename remove_reference<T>::type&&
move(T&& a)
{
return a;
}
Run Code Online (Sandbox Code Playgroud)
为什么remove_reference?有人可以给我一个简单的解释吗?
我正在阅读有关std::move移动构造函数和移动赋值运算符的内容.说实话,我现在得到的只是困惑.现在我有一节课:
class A{
public:
int key;
int value;
A(){key = 3; value = 4;}
//Simple move constructor
A(A&& B){ A.key = std::move(B.key);
A.value = std::move(B.value);}
};
Run Code Online (Sandbox Code Playgroud)
B是一个右值参考,为什么你可以申请std::moveravlue参考的成员?B.key和B.value已被移动,都已经失效,但如何B作为类的一个对象A被失效?A a(A()),A()显然是一个rvlaue,可以A()被移动std::move,为什么?同样,如果我有一个功能
int add(int && z){
int x = std:move(z);
int y = std:move(z);
return x+y;
}
如果我打电话add(5),怎么可以5移动,为什么?并且注意到z已经移动了两次,在z第一次移动之后,它已经失效,你怎么能再次移动它?
foo (T …我试图理解右值引用和移动语义.在下面的代码中,当我将10传递给Print函数时,它会调用rvalue reference overload,这是预期的.但究竟发生了什么,10将被复制(或从它引用的地方).其次,std::move究竟做了什么?它是从中提取值10 i然后通过它吗?或者是编译器使用右值参考的指令?
void Print(int& i)
{
cout<<"L Value reference "<<endl;
}
void Print(int&& i)
{
cout<<"R Value reference "<< endl;
}
int main()
{
int i = 10;
Print(i); //OK, understandable
Print(10); //will 10 is not getting copied? So where it will stored
Print(std::move(i)); //what does move exactly do
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
I tried implementing std::move, which uses std::remove_reference, however it seems to work without it. Please, give me an example in which my implementation will fails whithout std::remove_reference.
template <class type> type && move(type & source) { return (type &&) source; }
template <class type> type && move(type && source) { return (type &&) source; }
Run Code Online (Sandbox Code Playgroud)
Is std::remove_reference used only to avoid overloading std::move?
Here is a test class to help you:
class test {
public : …Run Code Online (Sandbox Code Playgroud) 如你所知,_Remove_reference存在,将T&转换为T或T &&转换为T.
我以一种俏皮的心情制作了以下代码,它完全不像我预期的那样工作,但不知道为什么.
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; }
// }; …Run Code Online (Sandbox Code Playgroud) 以下代码使VC2010失败:
//code1
std::string& test1(std::string&& x){
return x;
}
std::string str("xxx");
test1(str); //#1 You cannot bind an lvalue to an rvalue reference
//code2
std::string&& test1(std::string&& x){
return x; //#2 You cannot bind an lvalue to an rvalue reference
}
Run Code Online (Sandbox Code Playgroud)
有一些文章可以解释#1,但我不明白为什么#2也会失败.
让我们看看std :: move是如何实现的
template<class _Ty> inline
typename tr1::_Remove_reference<_Ty>::_Type&&
move(_Ty&& _Arg)
{ // forward _Arg as movable
return ((typename tr1::_Remove_reference<_Ty>::_Type&&)_Arg);
}
Run Code Online (Sandbox Code Playgroud)
什么是std的魔力:移动?
谢谢
#include <type_traits>
template<class T>
typename std::remove_reference<T>::type&& move(T&& v)
{
return v;
}
void main()
{
int a;
move(a);
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码不能编译?
错误C2440:'return':无法在'int &&'中转换'int'
这是std::move(). 它不完全符合标准的细节,但非常接近:
template<class T>
typename std::remove_reference<T>::type&&
myMove( T&& Arg )
{
return ( ( typename std::remove_reference<T>::type&& )Arg );
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这是行不通的,如果我们更换typename std::remove_reference<T>::type&&由T&&,即
template<class T>
typename std::remove_reference<T>::type&&
myMove( T&& Arg )
{
return ( (T&&) Arg );
}
Run Code Online (Sandbox Code Playgroud) 在cpp 参考中的以下示例中:
#include <iostream>
#include <utility>
#include <vector>
#include <string>
int main()
{
std::string str = "Hello";
std::vector<std::string> v;
// uses the push_back(const T&) overload, which means
// we'll incur the cost of copying str
v.push_back(str);
std::cout << "After copy, str is \"" << str << "\"\n";
// uses the rvalue reference push_back(T&&) overload,
// which means no strings will be copied; instead, the
// Contents of str will be moved into the vector. This is
// less …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×8
move ×2
c++-faq ×1
expression ×1
reference ×1
stdmove ×1
syntax ×1
visual-c++ ×1