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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:06:53+00:00 2026-05-24T19:06:53+00:00

Seeing as C++11 supports move semantics, when initializing data members from arguments, should we

  • 0

Seeing as C++11 supports move semantics, when initializing data members from arguments, should we attempt to move the value instead of copying it?

Here’s an example showing how I would approach this in pre-C++11:

struct foo {
    std::vector<int> data;

    explicit foo(const std::vector<int>& data)
        : data(data)
    {
    }
};

Here, the copy constructor would be called.

In C++11, should we get into the habit of writing like this:

struct foo {
    std::vector<int> data;

    explicit foo(std::vector<int> data)
        : data(std::move(data))
    {
    }
};

Here, the move constructor would be called… as well as the copy constructor if the argument passed is an lvalue, but the benefit is that if an rvalue was passed, the move constructor would be called instead of the copy one.

I’m wondering if there’s something I’m missing.

  • 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-24T19:06:54+00:00Added an answer on May 24, 2026 at 7:06 pm

    My initial answer to your question was:

    Don’t copy data that you want to move. You can add a constructor using a rvalue reference, if performance is a problem:

    explicit foo(std::vector<int>&& data)
        : data(std::move(data))            // thanks to Kerrek SB
    {
    }
    

    Not exactly matching your question, but reading
    Rule-of-Three becomes Rule-of-Five with C++11?
    seems to be useful.

    Edit:

    However, the accepted answer to
    Passing/Moving parameters of a constructor in C++0x
    seems to advocate your approach, especially with more than one parameter.
    Otherwise there would be a combinatorial explosion of variants.

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

Sidebar

Related Questions

Seeing lots of SOAP/RDS stuff, but just want to dump some MySQL data and/or
According to everything I've read, Firefox 3.5+ supports localStorage. And yet I'm seeing the
I have a data object used to contain my UI data that supports INotifyPropertyChanged
I know that HTML5 supports custom data-* attributes , and I know that VS2010
I'm trying to upgrade some tests as we move our app from Rails 2
Just seeking confirmation here : apache Thrift protocol does not seem to support running
Seeing as Java doesn't have nullable types, nor does it have a TryParse(), how
Seeing this: http://www.suckless.org/wiki.html . A wiki based on Mercurial. Are there any other non-code
Seeing various locking related question and (almost) always finding the 'loop because of spurious
Seeing some strange things; help is being solicited. I have a query, like so:

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.