Similar Posts

30 thoughts on “C++11 Move Semantics
  1. "Fundamental Concepts in Programming Languages", I think this paper by Christopher Strachey is a gentle introduction to theoretical part of programming languages which helps to clarify all these C++ concepts. It also introduced the idea of lvalues and rvalues, ad-hoc and parametric polymorphism etc. I guess in 1967.

  2. Good video, but the rule of three which you have explained at 17:50 is not correct. Before C++11 it was rule of three. With new standard rule of five was introduced. Generic definition would be: if you must define any of the special member functions you should probably define all of them.

    Also there is also mistake at 19:26. Standard says that move constructor and move operator can be generated (but there are rules for that). Not all compilers have this feature. I think that the Visual Studio 2017 compiler will generate them but one of the previous versions (2013 or 2015) didn't generated them.

  3. Minor quibble. Regarding the new in C++ 11 "initializer syntax available everywhere and is now preferred". Originally I just thought it was terrific that I didn't need to create an otherwise unnecessary array in order to initialize my vector and other such blemishes in C++ 98 being removed.

    Then I thought "Great, now people won't keep confusing assignment with copy construction when they are sloppy or rushing, because we will always say

    TYPE var { initial value }
    instead of:
    TYPE var = initial value

    which sort of looks like an assignment but actually calls a constructor."

    At about 3:18 or so, you use the old syntax of copy constructor which calls a copy constructor but looks kinda assignment-y. Have you fully switched over to using { } syntax when calling copy constructors? I still sometimes find myself writing calls to copy constructors like that, but most modern C++ videos seem to totally avoid it because of the historical ambiguity due to looking like an assignment. Or because they only learned C++ recently and that's the way they always did it. I know both work, but this is a modern C++ video, so I think this may be a relevant point.

    Now back to your video.

  4. Regarding lvalues and rvalues early in the video. I would completely avoid showing 1 + 1 = 2 for the following reason. We get bit by the meaning of "=" in C/C++/etc. etc. as the assignment operator which mathematicians like Wirth and Dijkstra always hated, because for 600 years "=" meant what we now use "==" for in these languages. If we were using a language that used ":=" for assignment it would be different, but we aren't.

    Secondly, people need to get away from thinking of lvalues as things appearing on the left hand side of an assignment and rvalues as things appearing on the right hand side of an assignment, even me. It might be best to just say "we used to think of these as which side of an assignment they were on, but really what we mean is that an lvalue always has an address and an rvalue either has none at all because it is a pure temporary, or has one for just a moment but it is about to expire so we treat it as if it didn't….

  5. I'm never crazy about having identical names for constructor parameters and members, and I have been looking for the best standard to adopt. I'm definitely not crazy about qualifying the member names with "this->" as I've seen in some videos, but calling the parameter, say, ii so the constructor initializer looks like i(ii) isn't really ideal to me either. However, the solution of using v both for the data member and the parameter indeed gets rather confusing right at the end of the video.

    "This is my cousin Daryl, this is my other cousin Daryl." That worked fine on that show (whichever it was) but that was as a joke.

  6. You've done it again! Excellent video.

    There is only one corner case I want to ask about that you didn't make crystal clear.

    struct Structure { int i; };
    Structure myStructure{45}; // same with Structure myStructure = Structure{45}; if the constructor is not marked explicit and here it's all good
    Structure&& rrStructure = std::move(myStructure); // oneproduct what is rrStructure here is it an lvalue or an xvalue?

    The expression std::move(myStructure) yields an xvalue correct?, but it is said that "you can't move an lvalue". (of course in order to move you need move ctor or move assignment operators and they are implicitly generated in struct Structure in this case).

    It is said that xvalues may or many not have an identity (ie. as you correctly pointed out an address, or a name).
    rrStructure has type rvalue reference.
    rrStructure has an identity.

    2 Questions:
    1. What about rrStructure's value category? That is what I want to know.

    2. Is the std::move(myStructure) valid there? Iis myStructure left in a valid unspecified state after the std::move?

    Thanks!

  7. So if I have a custom STL vector class with copy ctor, copy operator, move ctor and move operator, to be more save I should say
    vector(vector<T>&& src) { operator=(std::move(src)); }
    instead of
    vector(vector<T>&& src) { operator=(src); }

    but it will not touch `vector<T>&& src` anywhere inside this ctor.
    Does it make any difference or it is just a dignified way?

  8. I did the same program in VS2015 and if constructor and virtual destructor is defined then (move constructor ,move assignment operator, copy constructor and assignment operator are provided by compiler) . Program worked fine. ?
    if constructor ,virtual destructor ,copy constructor and assignment operator are defined then also (move constructor ,move assignment operator are provided by compiler) . Program worked fine.?
    But yes if we define move constructor ,move assignment operator then we have to manually define copy constructor and assignment operator.

  9. Would you need to free resources of Foo f (if any) before assigning it to a new object using the Rvalue Assignment Constructor?

    Consider the following:

    Foo f;

    {
    f=Foo(5);
    };

    f now stole the resources of Foo(5). But would this create a memory leak if f already had resources.

Comments are closed.

WP2Social Auto Publish Powered By : XYZScripts.com