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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:15:26+00:00 2026-06-01T15:15:26+00:00

How to write a template, that would take as argument classes, whose constructors have

  • 0

How to write a template, that would take as argument classes, whose constructors have mutually exclusive signatures?

class A
{
  A(){};
public:
  int a;
  A(int i) : a(i) {};
};

class B{
  B(){};
public:
  int a,b;
  B(int i,int j) : a(i), b(j) {};
};


template <class T> class C {
public:
  T* _t;
  C(int i[])
  {                       //???
    _t=new T(i[0]);       //if T has T(int) signature
    _t=new T(i[0],i[1]);  //if T has T(int,int) signature
  }
  ~C() {delete _t;}
};

int main()  
{ 
  int Ai[]={1,2};
  C<A> _c(Ai);  // template should work instantiated with A and B
  C<B> _c(Ai);  // 
  return 0;
}

The signatures of A and B are fixed (cannot be changed to int[] e.g.). Context: I’m thinking about a wrapper, that would take a (specialized) container type as the template argument, e.g. T=vector<int>, or T=map<int,int>, and the problem arises when constructors need to be called.

  • 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-01T15:15:28+00:00Added an answer on June 1, 2026 at 3:15 pm

    Use a variadically-templated constructor:

    template <typename T> struct C
    {
        template <typename ...Args> C(Args &&... args)
        : _t(new T(std::forward<Args>(args)...))
        {
        }
    
        // ... destructor? Rule of five? Don't use pointers!!!
    private:
        T * _t; // ouch!
    };
    

    Usage:

    C<A> x(1);
    C<B> y(2, 3);
    

    (Real programmers would of course prefer a member std::unique_ptr<T> _t;, with otherwise unchanged semantics, but allowing you to disregard all the comments.)

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

Sidebar

Related Questions

We have some code that looks like this: class Serializer { public: template<class Type>
How would you write a template function that takes a variable number of homogeneous
I have that template class that uses a policy for it's output and another
I would like to write a template that will determine if a type is
Normally when you write a template class and have a specialization of a function
Is it possible to write a template that changes behavior depending on if a
I am trying to write a template class in C++ and getting this strange
I know how to write a variadic template function that accepts a variable number
I have a template that displays the list of items. It has one checkbox
I have a custom template, and I want to somehow take the output of

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.