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

  • Home
  • SEARCH
  • 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 7896813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:59:26+00:00 2026-06-03T07:59:26+00:00

What does it take to use the move assignment operator of std::string (in VC11)?

  • 0

What does it take to use the move assignment operator of std::string (in VC11)?

I hoped it’d be used automatically as v isn’t needed after the assignment anymore.
Is std::move required in this case? If so, I might as well use the non-C++11 swap.

#include <string>

struct user_t
{
    void set_name(std::string v)
    {
        name_ = v;
        // swap(name_, v);
        // name_ = std::move(v);
    }

    std::string name_;
};

int main()
{
    user_t u;
    u.set_name("Olaf");
    return 0;
}
  • 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-03T07:59:27+00:00Added an answer on June 3, 2026 at 7:59 am

    I hoped it’d be used automatically as v isn’t needed after the assignment anymore. Is std::move required in this case?

    Movement always must be explicitly stated for lvalues, unless they are being returned (by value) from a function.

    This prevents accidentally moving something. Remember: movement is a destructive act; you don’t want it to just happen.

    Also, it would be strange if the semantics of name_ = v; changed based on whether this was the last line in a function. After all, this is perfectly legal code:

    name_ = v;
    v[0] = 5; //Assuming v has at least one character.
    

    Why should the first line execute a copy sometimes and a move other times?

    If so, I might as well use the non-C++11 swap.

    You can do as you like, but std::move is more obvious as to the intent. We know what it means and what you’re doing with it.

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

Sidebar

Related Questions

I need to use a function on a vector that does not take a
In Oracle (10g), when I use a View (not Materialized View), does Oracle take
A different question inspired the following thought: Does std::vector<T> have to move all the
I use a scheduler to ensure that my loop does not take too long,
Why does it take forever for ASP.NET to process a request the first time
On average, how long does it take for Debian to package new versions of
I know dbsight allows synonyms and stop words for searching but does this take
How long does it usually take for individual developers to be accepted into the
Does doing good enough software take anything from you being a programmer? Here are
Does anyone know how to take a screencap and download it to desktop in

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.