相关疑难解决方法(0)

什么是右值,左值,x值,glvalues和prvalues?

在C++ 03中,表达式是rvaluelvalue.

在C++ 11中,表达式可以是:

  1. 右值
  2. 左值
  3. x值
  4. glvalue
  5. prvalue

两类已成为五大类.

  • 这些新的表达类别是什么?
  • 这些新类别如何与现有的左值和左值类别相关联?
  • C++ 0x中的右值和左值类别是否与它们在C++ 03中的相同?
  • 为什么需要这些新类别?是WG21神只是想迷惑我们凡人?

c++ expression c++-faq c++11

1291
推荐指数
13
解决办法
17万
查看次数

移动语义std :: move

我不太了解这个std::move功能

template <class T>
typename remove_reference<T>::type&&
move(T&& a)
{
    return a;
}
Run Code Online (Sandbox Code Playgroud)

为什么remove_reference?有人可以给我一个简单的解释吗?

c++ move-semantics c++11

12
推荐指数
2
解决办法
806
查看次数

移动构造函数和std :: move混淆

我正在阅读有关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)
  1. 我认为这B是一个右值参考,为什么你可以申请std::moveravlue参考的成员?
  2. 之后B.keyB.value已被移动,都已经失效,但如何B作为类的一个对象A被失效?
  3. 如果我有A a(A()),A()显然是一个rvlaue,可以A()被移动std::move,为什么?
  4. 同样,如果我有一个功能

    int add(int && z){ int x = std:move(z); int y = std:move(z); return x+y; }

如果我打电话add(5),怎么可以5移动,为什么?并且注意到z已经移动了两次,在z第一次移动之后,它已经失效,你怎么能再次移动它?

  1. 在定义foo (T …

c++ move rvalue-reference c++11

7
推荐指数
1
解决办法
4215
查看次数

当我们使用右值引用时,究竟会发生什么?std :: move如何工作?

我试图理解右值引用和移动语义.在下面的代码中,当我将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)

谢谢.

c++ rvalue-reference move-semantics c++11

6
推荐指数
2
解决办法
1570
查看次数

Why is std::remove_reference used in std::move?

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)

c++ syntax reference move-semantics

6
推荐指数
1
解决办法
1065
查看次数

为什么没有_Remove_reference,std :: move()不起作用?

如你所知,_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)

c++ rvalue-reference c++11

5
推荐指数
1
解决办法
455
查看次数

什么是标准的魔力:移动

以下代码使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)
  1. move的参数仍然是右值引用,但move(str)是可以的!
  2. move也返回rvalue.

什么是std的魔力:移动?

谢谢

c++ rvalue-reference c++11

5
推荐指数
1
解决办法
2190
查看次数

移动语义std :: move如何使用它

#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'

c++ rvalue-reference visual-c++ move-semantics c++11

3
推荐指数
1
解决办法
2362
查看次数

无法理解 std::move 的实现

这是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)

c++ move-semantics

3
推荐指数
1
解决办法
1493
查看次数

std::move 如何使原始变量的值无效?

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++ move move-semantics c++11 stdmove

2
推荐指数
2
解决办法
1726
查看次数