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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:25:35+00:00 2026-05-27T23:25:35+00:00

How would a constructor and a copy constructor look for this variadic template class?

  • 0

How would a constructor and a copy constructor look for this variadic template class?

struct A {};
struct B {};

template < typename Head,
           typename... Tail>
struct X : public Head,
           public Tail...
{
    X(int _i) : i(_i) { }

    // add copy constructor

    int i;
};

template < typename Head >
struct X<Head> { };

int main(int argc, const char *argv[])
{
    X<A, B> x(5);
    X<A, B> y(x);

    // This must not be leagal!
    // X<B, A> z(x);

    return 0;
}
  • 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-27T23:25:36+00:00Added an answer on May 27, 2026 at 11:25 pm
    template < typename Head,
               typename... Tail>
    struct X : public Head,
               public Tail...
    {
        X(int _i) : i(_i) { }
    
        // add copy constructor
        X(const X& other) : i(other.i) {}
    
        int i;
    };
    

    Inside the template class, X as a type means X<Head, Tail...>, and all X with different template parameters are distinct types, so the copy constructor of X<A,B> won’t match X<B,A>.

    Demo: http://ideone.com/V6g35

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

Sidebar

Related Questions

class MyString { public: MyString(int length):_ptr(alloca(length)) { } //Copy Constructor, destructor, other member functions.
I am writing matrix classes. Take a look at this definition: template <typename T,
I would expect an error inside the copy constructor, but this compiles just fine
With the code: template <typename T> class MyClass { public: MyClass(const MyClass& other) {
I asked this question about overloading the copy constructor and assignment operator with template
$12.8/2 - 'A non-template constructor for class X is a copy constructor if its
What would be the best way to write a generic copy constructor function for
How would you call the constructor of the following class in these three situations:
I would like to pass values into the constructor on the class that implements
Let's assume I have a c++ class that have properly implemented a copy constructor

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.