Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numeric vector operator overload+ rvalue reference parameter

I have the numeric vector template class below (vector for numerical calculations). I am trying make it possible to write D=A+B+C where all variables are Vector objects. A, B and C should not be modified. My idea is to use Vector operator+(Vector&& B) so that after (hopefully) an Rvalue Vector has been returned from B+C, all subsequent additions are stored in that object i.e. steal the storage of the Rvalue for all subsequent additions. This is in order to eliminate creation of new objects and required storage.

My problem is that I can see from output statements from each function called that Vector operator+(Vector&& B) is never called. I cannot understand why since if I have an overloaded dummy function foo(Vector& B) and foo(Vector&& B) and try foo(A+B+C), then the second function is called exactly as I hoped.

Sorry for the long winded question but this is my first question here and I want to try to be as clear as possible.

Any suggestions as to what I am obviously doing wrong or why I should not be trying this, would be appreciated.

template <typename T>
class Vector
{
        int n;
        T* v;
        Vector();
        ~Vector();
        Vector(const Vector& B);
        Vector(Vector&& B);
        inline Vector operator+(const Vector& B) const;
        inline Vector operator+(Vector&& B) const;
};

template <typename T>
Vector<T>::Vector(const Vector<T>& B)
{
        ...
}

template <typename T>
Vector<T>::Vector(Vector<T>&& B)
{
        ...
}

template <typename T>
Vector<T> Vector<T>::operator+(const Vector<T>& B) const
{
        Vector<T> C;
        ...
        return C;
}

template <typename T>
Vector<T> Vector<T>::operator+(Vector<T>&& B) const
{
        ...do stuff to B
        return B;
}
like image 769
Lars Chr Avatar asked Jul 30 '12 17:07

Lars Chr


People also ask

What are Lvalues and Rvalues in C++?

Lvalues and rvalues are fundamental to C++ expressions. Put simply, an lvalue is an object reference and an rvalue is a value. The difference between lvalues and rvalues plays a role in the writing and understanding of expressions.

What is R value reference in C++?

“r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable. They are declared using the '&' before the name of the variable.

What does && mean in C++?

The && (logical AND) operator indicates whether both operands are true. If both operands have nonzero values, the result has the value 1 . Otherwise, the result has the value 0 . The type of the result is int . Both operands must have an arithmetic or pointer type.

Can a function return an rvalue?

Typically rvalues are temporary objects that exist on the stack as the result of a function call or other operation. Returning a value from a function will turn that value into an rvalue. Once you call return on an object, the name of the object does not exist anymore (it goes out of scope), so it becomes an rvalue.


2 Answers

In the expression:

D=A+B+C

A and B are lvalues, so the call A+B calls Vector::operator(const Vector&)

That returns an rvalue, let's call it tmp, so the next sub-expression is tmp+C.

C is also an lvalue, so it calls Vector::operator(const Vector&) again. That returns another rvalue, lets call it tmp2

The final sub-expression is D=tmp2, but your type doesn't have a move-assignment operator, so the implicitly-defined copy-assignment operator is used.

i.e. you never invoke operator+ with an rvalue on the right-hand side, and the only expression which does have an rvalue argument is an assignment which you haven't defined for rvalues.

It would be better to define overloaded non-member operators:

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

This will work for any combination of rvalues and lvalues. (In general operator+ should usually be a non-member anyway.)

Edit: the alternative suggestion below doesn't work, it results in ambiguities in some cases.

Another alternative, if your compiler supports it (I think only clang does,) would be to keep your existing Vector::operator+(Vector&&) but replace your Vector::operator+(const Vector&) with two overloads distinguished by a ref-qualifier:

Vector Vector::operator+(const Vector& v) const&
{
  Vector tmp(*this);
  tmp += v;
  return tmp;
}

Vector Vector::operator+(const Vector& v)&&
{
  *this += v;
  return std::move(*this);
}

This reuses *this when it's known to be an rvalue, i.e. it uses move semantics when the left-hand side of the addition is an rvalue, compared to your original code which can only use move semantics when the right-hand side is an rvalue. (N.B. the code above assumes you've defined a member operator+= as suggested in David Rodriguez's answer)

like image 114
Jonathan Wakely Avatar answered Sep 29 '22 11:09

Jonathan Wakely


I would suggest that you provide operator+= as a member method and then reuse that from a free function operator+ defined as:

template <typename T>
Vector<T> operator+( Vector<T> lhs,              // by value
                     Vector<T> const & rhs ) {
    lhs += rhs;
    return lhs;
}

Given a call a + b + c, which groups as (a+b) + c, the compiler will create a copy of a for the first call to operator+, modify that in place (lhs += rhs) and then move it into the returned value. It will then move that returned value (unless it elides the move) into the argument of the second operator+, where it will be modified in place again, and then moved to the return value.

Overall, a single new object will be created, holding the result of a+b+c, providing semantics equivalent to:

Vector<T> tmp = a;
tmp += b;
tmp += c;

But with the nicer, more compact syntax a + b + c.


Note that this will not handle a + (b+c) graciously, and will create two objects, if you want to support that, you will need to produce multiple overloads.

like image 35
David Rodríguez - dribeas Avatar answered Sep 29 '22 13:09

David Rodríguez - dribeas