1

I am going over A Tour of C++ (Section 5.2 Copy and Move). Following the instructions in the book, I have built a container called Vector (it mimics std::vector). My goal is to efficiently implement the following (element-wise) summation:

Vector r = x + y + z;

According to the book, if I don't have move assignment and constructor, + operator will end up copying Vectors unnecessarily. So I implemented move assignment and constructor, but I think the compiler still doesn't use them when I run Vector r = x + y + z;. What am I missing? I appreciate any feedback. Below is my code. I expect to see an output Move assignment, but I get no output. (The summation part works, it's just the move business that I am not sure)

Code

// Vector.h
class Vector{

public:
    explicit Vector(int);
    Vector(std::initializer_list<double>);
    // copy constructor
    Vector(const Vector&);
    // copy assignment
    Vector& operator=(const Vector&);
    // move constructor
    Vector(Vector&&);
    // move assignment
    Vector& operator=(Vector&&);
    ~Vector(){delete[] elem;}
    double& operator[](int) const;
    int size() const;
    void show();
    friend std::ostream& operator<< (std::ostream& out, const Vector& vec);

private:
    int sz;
    double* elem;
};

Vector operator+(const Vector&,const Vector&);

// Vector.cpp

Vector::Vector(std::initializer_list<double> nums) {
    sz = nums.size();
    elem = new double[sz];
    std::initializer_list<double>::iterator it;
    int i = 0;
    for (it=nums.begin(); it!=nums.end(); ++it){
        elem[i] = *it;
        ++i;
    }
}

Vector::Vector(Vector&& vec) {
    sz = vec.sz;
    vec.sz = 0;
    elem = vec.elem;
    vec.elem = nullptr;
    std::cout<<"Move constructor"<<std::endl;
}

Vector& Vector::operator=(Vector&& vec) {
    if (this == &vec){
        return *this;
    }
    sz = vec.sz;
    vec.sz = 0;
    elem = vec.elem;
    vec.elem = nullptr;
    std::cout<<"Move assignment"<<std::endl;
    return *this;

Vector operator+(const Vector& vec1, const Vector& vec2){
    if (vec1.size() != vec2.size()){
        throw std::length_error("Input vectors should be of the same size");
    }
    Vector result(vec1.size());
    for (int i=0; i!=vec1.size(); ++i){
        result[i] = vec1[i]+vec2[i];
    }
    return result;
}
}
// Main
int main() {
    Vector x{1,1,1,1,1};
    Vector y{2,2,2,2,2};
    Vector z{3,3,3,3,3};
    Vector r = x + y + z;
} // Here I expect the output: Move assignment, but I get no output.
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
iamvegan
  • 482
  • 4
  • 15
  • Which part of the line would you expect to perform move *assignment*? You need to create temporary objects because of `operator+` and `Vector r` is constructed from that temporary object – UnholySheep Jun 17 '20 at 22:07
  • Your move-assignment operator leaks memory. – PaulMcKenzie Jun 17 '20 at 22:09
  • @PaulMcKenzie I can't see the leak, can you explain please? – iamvegan Jun 17 '20 at 22:15
  • `elem = vec.elem;` -- What happened to the "old" elem? Seems like you didn't `delete []` it. – PaulMcKenzie Jun 17 '20 at 22:15
  • @UnholySheep I think the compiler first computes this temporary `result` from the operation `x+y`. Then this `result` should be `move`d to the next operation `result+z`. At the end, the final result should be `move`d to `r`. This is my understanding. – iamvegan Jun 17 '20 at 22:20
  • Your `operator+` takes `const Vector&` parameters that can hold temporary objects - there is no need to move anything here. As for the creation of `r` - that move might be optimized out by the compiler (copy elision) – UnholySheep Jun 17 '20 at 22:23
  • @PaulMcKenzie I was leaving the deletion to the destructor. Shouldn't that be alright? The implementation of the move constructor is from the book. I implemented the move assignment similar to that. – iamvegan Jun 17 '20 at 22:30
  • @PaulMcKenzie this is what the book says "After a move, a moved-from object should be in a state that allows a destructor to be run." – iamvegan Jun 17 '20 at 22:31
  • 1
    As soon as that line is executed, you've lost the original address that `elem` was pointing to. How are you going to `delete[]` that memory? What you should have done is `std::swap` the `elem` values, so that the passed-in object gets to destroy the old data. For the move-assignment, you want to swap out the data instead of directly replacing it with `nullptr` (like the move-constructor). – PaulMcKenzie Jun 17 '20 at 22:32
  • @PaulMcKenzie I just got what you meant. You are right! Thanks for explaining! – iamvegan Jun 17 '20 at 22:39
  • `x+y+z+w` should do a move (I think!) – M.M Jun 17 '20 at 23:15
  • Note there is no assignment or `operator=` involved at all in `Vector r = x + y + z;`. In this case, the `=` token just introduces the initializer expression for `r`. Assignment applies when the left-side object already exists. – aschepler Jun 18 '20 at 00:21

1 Answers1

2

There is a move elision takes place.

According to the C++ 17 Standard (12.8 Copying and moving class objects)

31 When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.122 This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

(31.3) — when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

So the move constructor is omitted. The second temporary object created by the expression x + y + z; is directly built as the obejct r.

Vector r = x + y + z;

Also take into account that there is move elision in the return statement of the operator

(31.1) — in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cvunqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

So the temporary object created in the expression x + y (within the operator) will be moved (that is there will be elision relative to the return statement - preceding quote) in the expression ( x + y ) + z where it will be used by constant lvalue reference and the new temporary obejct created in this expression will be built as r.

In whole in this code snippet

Vector x{1,1,1,1,1};
Vector y{2,2,2,2,2};
Vector z{3,3,3,3,3};
Vector r = x + y + z;

due to the move elision there will be created 5 objects of the class Vector.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • The result object of outer `operator+` being identical to `r` is not covered by this text, since C++17 (it is no longer a copy elision scenario, the result object is now `r` instead of a temporary). There is NRVO from the variable inside `operator+` implementation to the result object, which is a different clause to what you quoted (class.copy.elision/3) – M.M Jun 17 '20 at 23:20
  • also this is 15.8.3/1, not 12.8/31 – M.M Jun 17 '20 at 23:20
  • @M.M No, the quote is correct. The two move/copy elision can be applied. I provided the quote for the object r not for elision in the return statement. – Vlad from Moscow Jun 17 '20 at 23:23
  • That quote doesn't apply to `r`. There is no "temporary class object" involved , the result object of `operator+` is `r` – M.M Jun 17 '20 at 23:30
  • @M.M Here two kinds of elision are combined. – Vlad from Moscow Jun 17 '20 at 23:32
  • @HTNW You are mistaken. If you define the both, copy and move constructors as deleted the program will not work. If you define the move constructor alone as deleted then the copy constructor will be available. – Vlad from Moscow Jun 17 '20 at 23:39
  • @VladfromMoscow Oh, you're right. Sorry for the trouble. – HTNW Jun 17 '20 at 23:42