Are C++11 move semantics doing something new, or just making semantics clearer?

后端 未结 6 1130
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 04:29

I am basically trying to figure out, is the whole \"move semantics\" concept something brand new, or it is just making existing code simpler to implement? I am always interested

6条回答
  •  青春惊慌失措
    2021-02-01 04:51

    I would say yes because a Move Constructor and Move Assignment operator are now compiler defined for objects that do not define/protect a destructor, copy constructor, or copy assignment.

    This means that if you have the following code...

    struct intContainer
    {
        std::vector v;
    }
    
    intContainer CreateContainer()
    {
        intContainer c;
        c.v.push_back(3);
        return c;
    }
    

    The code above would be optimized simply by recompiling with a compiler that supports move semantics. Your container c will have compiler defined move-semantics and thus will call the manually defined move operations for std::vector without any changes to your code.

提交回复
热议问题