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

后端 未结 6 1075
佛祖请我去吃肉
佛祖请我去吃肉 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:40

    No answer is complete without a reference to Thomas Becker's painstakingly exhaustive write up on rvalue references, perfect forwarding, reference collapsing and everything related to that.

    see here: http://thbecker.net/articles/rvalue_references/section_01.html

    0 讨论(0)
  • 2021-02-01 04:42

    Since move semantics only apply in the presence of rvalue references, which are declared by a new token, &&, it seems very clear that they are something new.

    In principle, they are purely an optimizing techique, which means that: 1. you don't use them until the profiler says it is necessary, and 2. in theory, optimizing is the compiler's job, and move semantics aren't any more necessary than register.

    Concerning 1, we may, in time, end up with an ubiquitous heuristic as to how to use them: after all, passing an argument by const reference, rather than by value, is also an optimization, but the ubiquitous convention is to pass class types by const reference, and all other types by value.

    Concerning 2, compilers just aren't there yet. At least, the usual ones. The basic principles which could be used to make move semantics irrelevant are (well?) known, but to date, they tend to result in unacceptable compile times for real programs.

    As a result: if you're writing a low level library, you'll probably want to consider move semantics from the start. Otherwise, they're just extra complication, and should be ignored, until the profiler says otherwise.

    0 讨论(0)
  • 2021-02-01 04:43

    In the Turing Tar Pit, there is nothing new under the sun. Everything that move semantics does, can be done without move semantics -- it just takes a lot more code, and is a lot more fragile.

    What move semantics does is takes a particular common pattern that massively increases efficiency and safety in a number of situations, and embeds it in the language.

    It increases efficiency in obvious ways. Moving, be it via swap or move construction, is much faster for many data types than copying. You can create special interfaces to indicate when things can be moved from: but honestly people didn't do that. With move semantics, it becomes relatively easy to do. Compare the cost of moving a std::vector to copying it -- move takes roughly copying 3 pointers, while copying requires a heap allocation, copying every element in the container, and creating 3 pointers.

    Even more so, compare reserve on a move-aware std::vector to a copy-only aware one: suppose you have a std::vector of std::vector. In C++03, that was performance suicide if you didn't know the dimensions of every component ahead of time -- in C++11, move semantics makes it as smooth as silk, because it is no longer repeatedly copying the sub-vectors whenever the outer vector resizes.

    Move semantics makes every "pImpl pattern" type to have blazing fast performance, while means you can start having complex objects that behave like values instead of having to deal with and manage pointers to them.

    On top of these performance gains, and opening up complex-class-as-value, move semantics also open up a whole host of safety measures, and allow doing some things that where not very practical before.

    std::unique_ptr is a replacement for std::auto_ptr. They both do roughly the same thing, but std::auto_ptr treated copies as moves. This made std::auto_ptr ridiculously dangerous to use in practice. Meanwhile, std::unique_ptr just works. It represents unique ownership of some resource extremely well, and transfer of ownership can happen easily and smoothly.

    You know the problem whereby you take a foo* in an interface, and sometimes it means "this interface is taking ownership of the object" and sometimes it means "this interface just wants to be able to modify this object remotely", and you have to delve into API documentation and sometimes source code to figure out which?

    std::unique_ptr actually solves this problem -- interfaces that want to take onwership can now take a std::unique_ptr<foo>, and the transfer of ownership is obvious at both the API level and in the code that calls the interface. std::unique_ptr is an auto_ptr that just works, and has the unsafe portions removed, and replaced with move semantics. And it does all of this with nearly perfect efficiency.

    std::unique_ptr is a transferable RAII representation of resource whose value is represented by a pointer.

    After you write make_unique<T>(Args&&...), unless you are writing really low level code, it is probably a good idea to never call new directly again. Move semantics basically have made new obsolete.

    Other RAII representations are often non-copyable. A port, a print session, an interaction with a physical device -- all of these are resources for whom "copy" doesn't make much sense. Most every one of them can be easily modified to support move semantics, which opens up a whole host of freedom in dealing with these variables.

    Move semantics also allows you to put your return values in the return part of a function. The pattern of taking return values by reference (and documenting "this one is out-only, this one is in/out", or failing to do so) can be somewhat replaced by returning your data.

    So instead of void fill_vec( std::vector<foo>& ), you have std::vector<foo> get_vec(). This even works with multiple return values -- std::tuple< std::vector<A>, std::set<B>, bool > get_stuff() can be called, and you can load your data into local variables efficiently via std::tie( my_vec, my_set, my_bool ) = get_stuff().

    Output parameters can be semantically output-only, with very little overhead (the above, in a worst case, costs 8 pointer and 2 bool copies, regardless of how much data we have in those containers -- and that overhead can be as little as 0 pointer and 0 bool copies with a bit more work), because of move semantics.

    0 讨论(0)
  • 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<int> 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.

    0 讨论(0)
  • 2021-02-01 04:52

    TL;DR

    This is definitely something new and it goes well beyond just being a way to avoid copying memory.

    Long Answer: Why it's new and some perhaps non-obvious implications

    Move semantics are just what the name implies--that is, a way to explicitly declare instructions for moving objects rather than copying. In addition to the obvious efficiency benefit, this also affords a programmer a standards-compliant way to have objects that are movable but not copyable. Objects that are movable and not copyable convey a very clear boundary of resource ownership via standard language semantics. This was possible in the past, but there was no standard/unified (or STL-compatible) way to do this.

    This is a big deal because having a standard and unified semantic benefits both programmers and compilers. Programmers don't have to spend time potentially introducing bugs into a move routine that can reliably be generated by compilers (most cases); compilers can now make appropriate optimizations because the standard provides a way to inform the compiler when and where you're doing standard moves.

    Move semantics is particularly interesting because it very well suits the RAII idiom, which is a long-standing a cornerstone of C++ best practice. RAII encompasses much more than just this example, but my point is that move semantics is now a standard way to concisely express (among other things) movable-but-not-copyable objects.

    You don't always have to explicitly define this functionality in order to prevent copying. A compiler feature known as "copy elision" will eliminate quite a lot of unnecessary copies from functions that pass by value.

    Criminally-Incomplete Crash Course on RAII (for the uninitiated)

    I realize you didn't ask for a code example, but here's a really simple one that might benefit a future reader who might be less familiar with the topic or the relevance of Move Semantics to RAII practices. (If you already understand this, then skip the rest of this answer)

    // non-copyable class that manages lifecycle of a resource
    // note:  non-virtual destructor--probably not an appropriate candidate
    //        for serving as a base class for objects handled polymorphically.
    class res_t {
      using handle_t = /* whatever */;
      handle_t* handle;  // Pointer to owned resource
    public:
      res_t( const res_t& src ) = delete;            // no copy constructor
      res_t& operator=( const res_t& src ) = delete; // no copy-assignment
    
      res_t( res_t&& src ) = default;                // Move constructor
      res_t& operator=( res_t&& src ) = default;     // Move-assignment
    
      res_t();                                       // Default constructor
      ~res_t();                                      // Destructor
    };
    

    Objects of this class will allocate/provision whatever resource is needed upon construction and then free/release it upon destruction. Since the resource pointed to by the data member can never accidentally be transferred to another object, the rightful owner of a resource is never in doubt. In addition to making your code less prone to abuse or errors (and easily compatible with STL containers), your intentions will be immediately recognized by any programmer familiar with this standard practice.

    0 讨论(0)
  • 2021-02-01 04:58

    There is absolutely something new going on here. Consider unique_ptr which can be moved, but not copied because it uniquely holds ownership of a resource. That ownership can then be transferred by moving it to a new unique_ptr if needed, but copying it would be impossible (as you would then have two references to the owned object).

    While many uses of moving may have positive performance implications, the movable-but-not-copyable types are a much bigger functional improvement to the language.

    In short, use the new techniques where it indicates the meaning of how your class should be used, or where (significant) performance concerns can be alleviated by movement rather than copy-and-destroy.

    0 讨论(0)
提交回复
热议问题