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 7019493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:11:26+00:00 2026-05-27T23:11:26+00:00

How is the proper way to implement move semantics with operator+ ? Similarly to

  • 0

How is the proper way to implement move semantics with operator+? Similarly to how it works for std::string?

I have attempted the following, however I was hoping there was some more elegant and possibly more correct way to do it:

class path
{
    std::vector<std::string> path_;
public:

    path& path::operator+=(const path& other)
    {
        path_.insert(std::begin(path_), std::begin(other.path_), std::end(other.path_));
        return *this;
    }

    path& path::operator+=(path&& other)
    {
        path_.insert(std::begin(path_), std::make_move_iterator(std::begin(other.path_)), std::make_move_iterator(std::end(other.path_)));
        return *this;
    }
};

template<typename L, typename R>
typename std::enable_if<std::is_convertible<path, L>::value, path>::type operator+(const L& lhs, const R& rhs)
{
    auto tmp = std::forward<L>(lhs);
    tmp     += std::forward<R>(rhs);
    return tmp;
}
  • 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-05-27T23:11:27+00:00Added an answer on May 27, 2026 at 11:11 pm

    Way too complicated. 🙂 Just abide by the rule you already should’ve followed:

    • Take the lhs of operator+ by value
    • implement operator+ in terms of operator+= on the lhs

    This was already true in C++03, because of copy elision and RVO. The rule of thumb: If you make a copy anyways, make it in the parameters.

    With that in mind:

    #include <iterator>
    #include <utility>
    
    class path
    {
        std::vector<std::string> path_;
    public:
    
        path& operator+=(path other)
        {
            auto op_begin = std::make_move_iterator(std::begin(other.path_));
            auto op_end = std::make_move_iterator(std::end(other.path_));
            path_.reserve(path_.size() + other.path_.size());
            path_.insert(std::end(path_), op_begin, op_end);
            return *this;
        }
    };
    
    path operator+(path lhs, path rhs)
    {
      return std::move(lhs += std::move(rhs));
    }
    

    This should be the most optimal form. Note that I also changed your operator+= to actually append the path, and not prepend (I hope that’s what you had in mind. If not, feel free to change it to std::begin(path_) again).

    I also made the rhs of operator+ and operator+= values and then just moved them around. std::make_move_iterator is also a nice utility. As the name implies, instead of copying, it moves the pointed-at elements. This should really be as fast as it’s going to get.

    Another version might be to use the iterator version of std::move in operator+=:

    path& operator+=(path other)
    {
        path_.reserve(path_.size() + other.path_.size());
        std::move(other.begin(), other.end(), std::back_inserter(path_));
        return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling for days trying to find the proper way to implement
What is the proper way to implement assignment by value for a reference type?
What's the proper way to convert from a scientific notation string such as 1.234567E-06
What is the proper way to implement a getter method for a lazily-initialized member
What is the proper way to implement shadows with CoreGraphics? I've looked around but
What is the proper way to implement shadows with CoreGraphics? I've looked around but
TL;DR Is PinView.prototype = _.extend(PinView.prototype, google.maps.OverlayView.prototype) the proper way to have a Backbone View
What is the proper way to implement Custom Properties in Silverlight UserControls? Every "Page"
What is the proper way to implement this C# code: protected override void SomeMethod(inputs)
What is the most proper way to sending email of minimal 1000 or more

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.