I'm trying to understand how copy and move semantics work in a linear algebra vector class I'm writing with an overloaded operator+. I've intentionally deleted the move assignment operator to see what the compiler (Clang, C++17) will do when I add two Vector objects. Unfortunately, I'm having trouble understanding why the copy assignment operator is never called:
class Vector
{
public:
explicit Vector(std::vector<int> v) : data(v){};
// Default constructor.
Vector() = default;
// Copy operations.
Vector(const Vector&);
Vector& operator=(const Vector&);
// Move operations.
Vector(Vector&&) = delete;
Vector& operator=(Vector&&) = delete;
private:
std::vector<int> data;
std::size_t rows = 0;
std::size_t cols = 0;
};
// Copy constructor.
Vector::Vector(const Vector& v1)
{
data = v1.data;
rows = v1.rows;
cols = v1.cols;
std::cout << "Copy construction invoked." << std::endl;
}
// Copy assignment.
Vector& Vector::operator=(const Vector& v1)
{
data = v1.data;
rows = v1.rows;
cols = v1.cols;
std::cout << "Copy assignment invoked." << std::endl;
return *this;
}
// Operator+
Vector operator+(const Vector& v1, const Vector& v2)
{
// Send back a dummy vector.
Vector v3{{1,2,13}};
return v3;
}
The main.cpp file is:
int main()
{
std::vector<int> q{1,2,31};
// Explicitly construct w and copy construct r.
Vector w(q);
Vector r(w);
// Should be copy assignment if move operations are deleted?
Vector u = w + r;
return 0;
}
I would have expected if the move assignment operator is deleted then the copy assignment operator would have been used to fill in the Vector u. However I don't see "Copy assignment invoked" printed to std::cout at all. What am I missing?