C++ RVO/NRVO以及move语义的影响-阿里云开发者社区

开发者社区> 开发与运维> 正文

C++ RVO/NRVO以及move语义的影响

简介:

C++返回值优化和具名返回值优化是编译器的优化,在大多数情况下能提高性能,但是却难以受程序员控制。C++11中加入了move语义的支持,由此对RVO和NRVO会造成一定影响。下面以一段代码来说明。

RVO和NRVO在分别在copy/move construct,copy/move assignment八种简单情况,测试条件是g++ 4.8.2和clang++ 3.4,默认优化。

#include <iostream>
#include <vector>
#include <string>

struct Test {
    Test()
    {
        std::cout << "construct a Test object" << std::endl;
    }

    Test(const Test&)
    {
        std::cout << "copy construct  a Test object" << std::endl;
    }

    Test& operator=(const Test&)
    {
        std::cout << "copy assignment a Test object" << std::endl;
        return *this;
    }


    Test(Test&&)
    {
        std::cout << "move construct a Test object" << std::endl;
    }


    /*
    Test& operator=(Test &&t)
    {
        std::cout << "move assignment a Test object" << std::endl;
        return *this;
    }
    */

    ~Test()
    {
        std::cout << "destruct a Test object" << std::endl;
    }
};

Test getTest()
{
    return Test();
}

Test getTestWithName()
{
    Test temp;
    return temp;
}

int main()
{
    std::cout << "=============RVO==============" << std::endl; 
    std::cout << "++Test obj rvo for copy construct" << std::endl;
    auto obj1 = getTest();

    std::cout << "--------------" << std::endl;
    std::cout << "++Test obj rvo for move construct" << std::endl;
    auto obj111 = std::move(getTest());

    std::cout << "--------------" << std::endl;  
    std::cout << "++Test obj rvo for copy assignment" << std::endl;
    Test obj11; obj11 = getTest();

    std::cout << "--------------" << std::endl;
    std::cout << "++Test object rvo for move assignment" << std::endl;
    Test obj1111; obj1111 = std::move(getTest());


    std::cout << "=============NRVO==============" << std::endl; 
    std::cout << "++Test obj nrvo for copy construct" << std::endl;
    auto obj2 = getTestWithName();

    std::cout << "--------------" << std::endl;
    std::cout << "++Test obj nrvo for move construct" << std::endl;
    auto obj222 = std::move(getTestWithName());

    std::cout << "--------------" << std::endl;
    std::cout << "++Test obj nrvo for copy assignment" << std::endl;
    Test obj22; obj22 = getTestWithName();

    std::cout << "--------------" << std::endl;
    std::cout << "++Test obj nrvo for move assignment" << std::endl;
    Test obj2222; obj2222 = std::move(getTestWithName());

    std::cout << "==============================" << std::endl;
    // std::string s1 = "s1 string move semantics test", s2;
    //std::cout << "++before move s1\t" << s1 << std::endl;
    //s2 = std::move(s1);
    //std::cout << "++after move s1\t" << s1 << std::endl;
    //std::cout << "=============" << std::endl;
    return 0;
}

测试结果:

=============RVO==============
++Test obj rvo for copy construct
construct a Test object
--------------
++Test obj rvo for move construct
construct a Test object
move construct a Test object
destruct a Test object
--------------
++Test obj rvo for copy assignment
construct a Test object
construct a Test object
move assignment a Test object
destruct a Test object
--------------
++Test object rvo for move assignment
construct a Test object
construct a Test object
move assignment a Test object
destruct a Test object
=============NRVO==============
++Test obj nrvo for copy construct
construct a Test object
--------------
++Test obj nrvo for move construct
construct a Test object
move construct a Test object
destruct a Test object
--------------
++Test obj nrvo for copy assignment
construct a Test object
construct a Test object
move assignment a Test object
destruct a Test object
--------------
++Test obj nrvo for move assignment
construct a Test object
construct a Test object
move assignment a Test object
destruct a Test object
==============================
destruct a Test object
destruct a Test object
destruct a Test object
destruct a Test object
destruct a Test object
destruct a Test object
destruct a Test object
destruct a Test object

由此可得出几个简单结论:
1.copy construct本身在RVO和NRVO两种情况下被优化了,如果再加上move反而画蛇添足。
2.加入了move assignment后,默认是调用move assignment而不是copy assignment,可以将move assignment注释后测试。
3.对于RVO和NRVO来说,construction的情况编译器优化得比较好了,加入move语义主要是对于assignment有比较大影响

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章