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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:09:08+00:00 2026-06-03T16:09:08+00:00

Consider these classes: #include <iostream> #include <string> class A { std::string test; public: A

  • 0

Consider these classes:

#include <iostream>
#include <string>

class A
{
    std::string test;
public:
    A (std::string t) : test(std::move(t)) {}
    A (const A & other) { *this = other; }
    A (A && other) { *this = std::move(other); }

    A & operator = (const A & other)
    {
        std::cerr<<"copying A"<<std::endl;
        test = other.test;
        return *this;
    }

    A & operator = (A && other)
    {
        std::cerr<<"move A"<<std::endl;
        test = other.test;
        return *this;
    }
};

class B
{
    A a;
public:   
    B (A && a) : a(std::move(a)) {}
    B (A const & a) : a(a) {}
};

When creating a B, I always have an optimal forward path for A, one move for rvalues or one copy for lvalues.

Is it possible to achieve the same result with one constructor? It’s not a big problem in this case, but what about multiple parameters? I would need combinations of every possible occurrence of lvalues and rvalues in the parameter list.

This is not limited to constructors, but also applies to function parameters (e.g. setters).

Note: This question is strictly about class B; class A exists only to visualize how the copy/move calls gets executed.

  • 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-03T16:09:10+00:00Added an answer on June 3, 2026 at 4:09 pm

    The “by-value” approach is an option. It is not as optimal as what you have, but only requires one overload:

    class B
    {
        A a;
    public:   
        B (A _a) : a(move(_a)) {}
    };
    

    The cost is 1 extra move construction for both lvalues and xvalues, but this is still optimal for prvalues (1 move). An “xvalue” is an lvalue that has been cast to rvalue using std::move.

    You could also try a “perfect forwarding” solution:

    class B
    {
        A a;
    public:   
        template <class T,
                  class = typename std::enable_if
                  <
                     std::is_constructible<A, T>::value
                  >::type>
        B (T&& _a) : a(std::forward<T>(_a)) {}
    };
    

    This will get you back to the optimal number of copy/move constructions. But you should constrain the template constructor such that it is not overly generic. You might prefer to use is_convertible instead of is_constructible as I’ve done above. This is also a single constructor solution, but as you add parameters, your constraint gets increasingly complicated.

    Note: The reason the constraint is necessary above is because without, clients of B will get the wrong answer when they query std::is_constructible<B, their_type>::value. It will mistakenly answer true without a proper constraint on B.

    I would say that none of these solutions is always better than the others. There are engineering tradeoffs to be made here.

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

Sidebar

Related Questions

Consider these classes: public class BaseClass { public int SomeInt { get; set; }
Consider these two classes: public class A { B b; public A(B b) {
Consider these classes. class Base { ... }; class Derived : public Base {
To give an idea of my requirement, consider these classes - class A {
Consider these classes: class Parent { int a; } class Child extends Parent {
Consider these two classes mapped to the same table. One is readonly via mutable=false.
Consider these pseudo models: class BaseProduct: quantity_available = Integer class Box(BaseProduct): items_in_box = Integer
Consider the following code (C++11), which uses the Eigen 3 library (http://eigen.tuxfamily.org): #include <iostream>
Consider these two abstract classes. MyClass2 extends my BaseClass and overrides a virtual method.
Consider these 2 pieces of code (you can assume execeptionObj is of type Object

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.