std::move std::forward emplace_back() push_back()

要实现转移语义,需要定义转移构造函数,还可以定义转移赋值操作符。

MyString(“Hello”)是临时对象,也就是右值。虽然它们是临时的,但程序仍然调用了拷贝构造和拷贝赋值,造成了没有意义的资源申请和释放的操作。如果能够直接使用临时对象已经申请的资源,既能节省资源,有能节省资源申请和释放的时间。这正是定义转移语义的目的。

转移构造函数。

 MyString(MyString&& str) {

    _len = str._len;

    _data = str._data;

    str._len = 0;

    str._data = NULL;

 }

有下面几点需要对照代码注意: 
1. 参数(右值)的符号必须是右值引用符号,即“&&”。 
2. 参数(右值)不可以是常量,因为我们需要修改右值。 
3. 参数(右值)的资源链接和标记必须修改。否则,右值的析构函数就会释放资源。转移到新对象的资源也就无效了。

定义转移赋值操作符。

  MyString& operator=(MyString&& str) { 
    if (this != &str) { 
      _len = str._len; 
      _data = str._data; 
      str._len = 0; 
      str._data = NULL; 
    } 
    return *this; 
 }

编译器区分了左值和右值,对右值调用了转移构造函数和转移赋值操作符。

std::move的模拟实现:

template<typename T>                            //in namespace std

typename remove_reference<T>::type&&

move(T&& param)

{

    using ReturnType =                          // aliasdeclaration;

        typenameremove_reference<T>::type&&;    // see Item 9

    return static_cast<ReturnType>(param);

}

std::move将其参数转换右值

如果你的构造函数接受一个const对象

然后你还希望可以move

 explicitAnnotation(const std::string text)
  : value(std::move(text))   

那么:在const对象上的移动操作默默的被转换成了拷贝操作。

 

但std::move是无条件的转换其参数为一个右值,而std::forward是在某些特定条件下进行转换。

例子:process被重载为左值和右值。

voidprocess(const Widget& lvalArg);        // process lvalues

voidprocess(Widget&& rvalArg);            // processrvalues


template<typenameT>                       //template that passes

voidlogAndProcess(T&& param)               // param to process

{

auto now=                               // get current time


std::chrono::system_clock::now();


makeLogEntry("Calling'process'", now);


process(std::forward<T>(param));

}

       考虑两种调用logAndProcess的情形,一种是左值,一种是右值:

Widget w;

logAndProcess(w);             // call with lvalue

logAndProcess(std::move(w));   // call with rvalue

我们望那个左值可以同样作为一个左值转移到process函数,当我们通过右值去调用logAndProcess时,我们期望重载的右值函数可以被调用。

参见条款28

总结:

1.std::move执行一个无条件的转化到右值。它本身并不移动任何东西;

2.std::forward把其参数转换为右值,仅仅在那个参数被绑定到一个右值时;

3.std::move和std::forward在运行时(runtime)都不做任何事。

emplace_back() push_back() 的区别

在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放入容器中。原来的临时变量释放。这样造成的问题是临时变量申请的资源就浪费。 
引入了右值引用,转移构造函数后,push_back()右值时就会调用构造函数和转移构造函数。 
在这上面有进一步优化的空间就是使用emplace_back

例子:

struct President 

    std::string name; 

    std::string country; 

    int year; 

 

    President(std::string p_name, std::string p_country, int p_year) 

        : name(std::move(p_name)),country(std::move(p_country)),year(p_year) 

    { 

        std::cout << "I am being constructed.\n"

    }

    President(const President& other)

        : name(std::move(other.name)),country(std::move(other.country)),year(other.year)

    {

        std::cout << "I am being copyconstructed.\n";

    }

    President(President&& other) 

        : name(std::move(other.name)),country(std::move(other.country)),year(other.year) 

    { 

        std::cout << "I am being moved.\n"

    } 

    President& operator=(const President&other); 

}; 

调用:

std::vector<President> elections; 

    std::cout << "emplace_back:\n"

    elections.emplace_back("Nelson Mandela", "South Africa", 1994); //没有类的创建 

 

    std::vector<President> reElections; 

    std::cout << "\npush_back:\n"

    reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936)); 

输出:

emplace_back:

I am being constructed.

 

push_back:

I am being constructed.

I am being moved.

猜你喜欢

转载自blog.csdn.net/icebergliu1234/article/details/80602410
今日推荐