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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:41:22+00:00 2026-06-18T08:41:22+00:00

I have a struct defined as follows: struct A : public B, public C

  • 0

I have a struct defined as follows:

struct A : public B, public C
{
    A(const B& b) : B(b), C()
    {}

    template<typename... Args>
    A(Args&&... args) : B(), C(std::forward<Args>(args)...)
    {}
};

int main()
{
   B b;

   A sample1(b);
   A sample2(3); // For example, B has a B(int) constructor.
}

And this doesn’t work fine, because, A(b) tries use the second constructor (the non-constant reference is the preferred option, and the first constructor is a constant reference), but B hasn’t any B(A&).

And moreover, I want to add a move constructor for B:

struct A : public B, public C
{
    A(const B& b) : B(b), C()
    {}

    A(B&& b) : B(std::move(b)), C()
    {}

    template<typename... Args>
    A(Args&&... args) : B(), C(std::forward<Args>(args)...)
    {}
};

Now, the last step is to fusion the first two constructors:

struct A : public B, public C
{
   template<typename fw_B>
   A(fw_B&& b) : B(std::forward<fw_B>(b)), C()
   {}

   template<typename... Args>
   A(Args&&... args) : B(), C(std::forward<Args>(args)...)
   {}
 };

Question: if the first version causes collision, the last version (my final purpose) its clear that it doesn’t work also. How could I achieve this goal?

  • 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-18T08:41:23+00:00Added an answer on June 18, 2026 at 8:41 am

    A possible solution would be use std::enable_if with std::is_convertible to only include the first constructor if the type of argument b is convertible to B:

    template <
      class fw_B,
      class = typename std::enable_if<std::is_convertible<fw_B, B>::value, T>::type>
    A(fw_B&& b)
    

    For example:

    #include <iostream>
    #include <type_traits>
    
    struct B
    {
        B() {}
        B(int) {}
    };
    
    struct C {};
    
    struct A : B, C
    {
        template <
          class T,
          class = typename std::enable_if<std::is_convertible<T, B>::value, T>::type> 
        A(T&& t) { std::cout << "A(T&&)\n"; }
    
        template <class... TArgs>
        A(TArgs&&... targs) { std::cout << "A(TArgs&&)\n"; }
    };
    
    int main()
    {
        B b;
    
        A a1(b);
        A a2(4);
        A a3("hello");
    
        return 0;
    }
    

    Output:

    A(T&&)
    A(T&&)
    A(TArgs&&)
    

    See demo at http://ideone.com/xJEjic .

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

Sidebar

Related Questions

I have a struct defined as follows: template <typename T> struct data { int
I have an adjacency_list graph defined as follows: struct VertexProperties{ std::string name; ... };
I have an unordered_map<Block, int> with Block being a simple struct defined as follows:
I have a set of structs, defined as follows: typedef struct { int index;
I have a struct defined as: struct { char name[32]; int size; int start;
I have defined a struct ABC to contain an int ID, string NAME, string
I have a struct defined in a header as follows: #define LC_ERR_LEN 300 typedef
In a C struct I have defined a function pointer as follows: typedef struct
I have two vectors of MyObj structs. MyObj is defined as follows: struct MyObj
Assume I have an array which holds some struct defined as follows: static struct

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.