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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:23:13+00:00 2026-06-12T05:23:13+00:00

Say I have this constructor in C++: A::A( std::string const& name, std::string const& type,

  • 0

Say I have this constructor in C++:

 A::A( std::string const& name,
       std::string const& type,
       std::vector<B> const& b_vec,
       bool unique )
     : _name(name), _type(type), _b_vec(b_vec), _unique(unique)
     { };

I would like to overload this constructor for the case where the arguments are rvalues (I want to use move semantics there).

 A::A( std::string && name,
       std::string && type,
       std::vector<B> && b_vec,
       bool unique )
     : _name(name), _type(type), _b_vec(b_vec), _unique(unique)
     { };

The above one works fine when all of the arguments are rvalues, but suppose if only some of them are is in the next example:

 // create some lvalues somehow
 std::string name   = "stack overflow";
 std::vector<B> vec = { ... }; // implementation of B's constructot is not important

 // call a mixed constructor
 A new_A_instance(name, "cool-website", vec, true);

it is to my understanding that since ‘const&’ cannot bind to ‘&&’ but ‘&&’ can bind to ‘const&’ the first (non-move) constructor would be used.

This seems sub-optimal, since two of the four arguments could be moved (because they are rvalue) instead of being copied (as is the case in the first constructor).

So I could overload the operator for this specific case, but one could easily image a case where other arguments are rvalue and others are agin lvalue. Should I overload the constructor for each of these cases? This would combinatorily lead to very much overloads as the number of arguments increases…

I kind-of feel there is a better solution (perhaps using templates, but my template knowledge is shamefully low).

Note: this problem isn’t tied to overloading pass-by-ref functions to move functions per-se, but I found this a good example (especially since the overloads don’t feel very different). Also note that I just used constructors as an example, but the overloaded function can be anything.

  • 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-12T05:23:14+00:00Added an answer on June 12, 2026 at 5:23 am

    Pass by value, this is what move semantics are for:

     A::A(std::string name, std::string type, std::vector<B> b_vec, bool unique )
       : _name(std::move(name)), _type(std::move(type)), _b_vec(std::move(b_vec)),
         _unique(unique)
     { };
    

    This has the expected behaviour in every case. Passing a temporary by value allows the compiler to perform copy elision, which it pretty much always does.

    Note that in your second code, copies are made, since you don’t use std::move. Please realize that when you write

    void foo(bar&& x)
    {
        ...
    }
    

    then in the body of foo, x is a lvalue. Objects with names are always lvalues. Inside this body, you must use std::move(x) if you intend to pass x as a rvalue.

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

Sidebar

Related Questions

Say I have the following code: #include <string> using namespace std; string GetPath(const string
Say I have this constructor: /// <summary> /// Example comment. /// </summary> public SftpConnection(string
Lets say have this immutable record type: public class Record { public Record(int x,
Let's say I have an stl vector containing class type xx. xx is abstract.
Let's say I have a function: typedef std::vector<int> VecType; VecType randomVector(); int processing() {
Let say I have this example: class A : SomeAbstractClassWithProperties { public A(string id,
Say I have this class: class MyClass { private String s; // more attributes
I have a question about performance of move constructor, pseudo C++ class: typedef tuple<std::string,
Say I have this: private list<myClass> myCollection; Is there a programming idiom to shorten
Say i have this this structure. MyApp ├── main.py └── package ├── __init__.py ├──

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.