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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:22:01+00:00 2026-05-23T18:22:01+00:00

If I have a constructor with n parameters such that any argument to that

  • 0

If I have a constructor with n parameters such that any argument to that can be an rvalue and lvalue. Is it possible to do support this with move semantics for the rvalues without writing 2^n constructors for each possible rvalue/lvalue combination?

  • 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-23T18:22:02+00:00Added an answer on May 23, 2026 at 6:22 pm

    You take each one by value, like this:

    struct foo
    {
        foo(std::string s, bar b, qux q) :
        mS(std::move(s)),
        mB(std::move(b)),
        mQ(std::move(q))
        {}
    
        std::string mS;
        bar mB;
        qux mQ;
    };
    

    The initialization of the function parameters by the argument will either be a copy-constructor or move-constructor. From there, you just move the function parameter values into your member variables.

    Remember: copy- and move-semantics are a service provided by the class, not by you. In C++0x, you no longer need to worry about how to get your own “copy” of the data; just ask for it and let the class do it:

    foo f("temporary string is never copied", bar(), quz()); // no copies, only moves
    foo ff(f.mS, f.mB, f.mQ); // copies needed, will copy
    foo fff("another temp", f.mB, f.mQ); // move string, copy others
    

    Note: your constructor only takes in values, those values will figure out how to construct themselves. From there, of course, it’s up to you to move them where you want them.

    This applies everywhere. Have a function that needs a copy? Make it in the parameter list:

    void mutates_copy(std::string s)
    {
        s[0] = 'A'; // modify copy
    }
    
    mutates_copy("no copies, only moves!");
    
    std::string myValue = "don't modify me";
    mutates_copy(myValue); // makes copy as needed
    mutates_copy(std::move(myValue)); // move it, i'm done with it
    

    In C++03, you could emulate it fairly well, but it wasn’t common (in my experience):

    struct foo
    {
        foo(std::string s, bar b, qux q)
        // have to pay for default construction
        {
            using std::swap; // swaps should be cheap in any sane program
    
            swap(s, mS); // this is effectively what
            swap(b, mB); // move-constructors do now,
            swap(q, mQ); // so a reasonable emulation
        }
    
        std::string mS;
        bar mB;
        qux mQ;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it good practice to have a class constructor that uses default parameters, or
I have a service class which has overloaded constructors. One constructor has 5 parameters
I have a constructor (for an auto generated class) that has 255 paremeters. Using
I have a constructor like as follows: public Agent(){ this.name = John; this.id =
I have two constructor : function clsUsagerEmailUserName($nickName, $email) { $this->nickName = $nickName; $this->email =
I have a constructor that has a series of classes and functions within it
I have a constructor signature like this public NavigationLink(Func<String> getName, Func<UrlHelper, String> getURL, Func<bool>
I have a class with overloaded constructor (C#) It can be initialized in few
I have WCF services structured like suggested by Miguel Castro . This means that
I have a decorator-like pattern with a base that requires a constructor parameter. The

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.