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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:33:49+00:00 2026-05-19T15:33:49+00:00

Consider the following: struct X { Y y_; X(const Y & y) :y_(y) {}

  • 0

Consider the following:

struct X
{
    Y y_;

    X(const Y & y) :y_(y) {}    
    X(Y && y) :y_(std::move(y)) {}
};

Is it necessary to define a constructor like the second one in order to take full advantage of move semantics? Or will it be taken care of automatically in the appropriate situations?

  • 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-19T15:33:50+00:00Added an answer on May 19, 2026 at 3:33 pm

    Yes, but no. Your code should just be this:

    struct X
    {
        Y y_;
    
        X(Y y) : // either copy, move, or elide a Y
        y_(std::move(y)) // and move it to the member
        {} 
    };
    

    If you ever say in design “I need my own copy of this data”*, then you should just take the argument by value and move it to where it needs to be. It isn’t your job to decide how to construct that value, that’s up to the available constructors for that value, so let it make that choice, whatever it is, and work with the end result.

    *This applies to functions too, of course, for example:

    void add_to_map(std::string x, int y) // either copy, move or elide a std::string
    {
        // and move it to where it needs to be
        someMap.insert(std::make_pair(std::move(x), y));
    }
    

    Note that is applied in C++03 too, somewhat, if a type was default constructible and swappable (which is all moving does anyway):

    // C++03
    struct X
    {
        std::string y_;
    
        X(std::string y) // either copy or elide a std::string
        {
            swap(y_, y); // and "move" it to the member
        } 
    };
    

    Though this didn’t seem to be as widely done.

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

Sidebar

Related Questions

Consider the following code: struct Calc { Calc(const Arg1 & arg1, const Arg2 &
Consider the following code: struct Foo { }; struct Bar { explicit Bar(const Foo&)
Consider the following snippet: struct ObjectInterface { virtual ~ObjectInterface() {} virtual void Print(std::ostream& target)
Consider the following: struct Point {double x; double y;}; double complexComputation(const& Point p1, const
Please consider the following code, struct foo { foo() { std::cout << Constructing! <<
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
Consider I have Struct like the following: struct Bitmask { unsigned char payload_length: 7;
Consider the following snippet: struct Foo { static const T value = 123; //Where
Consider the following horribleness: #include <iostream> struct thing { thing() { std::cout << thing
Consider the following code: #include <unordered_map> struct A {}; struct T { std::unordered_map<std::string, A>

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.