Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:55:24+00:00 2026-06-08T21:55:24+00:00

I have the numeric vector template class below (vector for numerical calculations). I am

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:55:26+00:00Added an answer on June 8, 2026 at 9:55 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a (Numeric 3 float) vector class that I would love to align
I have a numeric vector (future_prices) in my case. I use a date vector
I have a numeric vector, it contains patches of elements that are repeating, something
I have a numeric vector in R, which consists of both negative and positive
I am writing a template Polynom<T> class where T is the numeric type of
So I have a list, each element of which is a numeric vector. Each
I have a numeric vector, let's say something like: x <- rep(1:6, 300) What
I have numeric data that I am getting from a database. They are all
I have a numeric vector implementation in C; the whole implementation is based on
Possible Duplicate: How to generate a vector containing a numeric sequence? In R, how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.