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

  • Home
  • SEARCH
  • 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 3271346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:43:11+00:00 2026-05-17T18:43:11+00:00

Is it possible to have a templated class and also templating the constructor with

  • 0

Is it possible to have a templated class and also templating the constructor with some other type?

something like this:

template<typename T1>
class Foo{
    template<typename T2>
    Foo(T1 aBar, T2 dummyArgument){
        bar = aBar;
        bytesOfT2 = sizeof(T2);
    };

    int bytesOfT2;
    T1 bar;
};

is this possible? and if so, how would I call such a constructor? Do I need to consider something in regards of header and cpp files?

thanks!

//edit: my particular example is actually even a little bit more complicated. i have

template <typename U1, U2>
class Foo{
    U1 var1;
    U2 var2;
};

template <typename T1>
class Bar{
    template<typename T2, typename T3>
    Bar(Foo<T2,T3> aFoo, T1 aVal){
        val=aVal;
        bytesOfT2=sizeof(T2);
        bytesOfT3=sizeOf(T3);
    };

int bytesOfT2;
int bytesOfT3;
T1 val;
};

does it mean i can here call the constructor just with any variable of type Foo and it should automatically select the proper Constructor acording to the particular version of Foo (for example if the variable i pass is of type Foo should it automatically set T2 to bool and T3 to float)?

  • 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-17T18:43:12+00:00Added an answer on May 17, 2026 at 6:43 pm

    Yes, a class template can have a constructor template. You call it as you would call any other constructor:

    Foo<int> my_foo(42, 0.0);
    

    This invokes the constructor template with T1 = int (because T1 is a class template parameter and the class template argument is int) and T2 = double (because T2 is a function template argument and is deduced from the argument 0.0).

    All of the template arguments have to be able to be deduced from the function arguments, otherwise the constructor template cannot be called. There is no way to explicitly specify the template arguments for a constructor template.

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

Sidebar

Related Questions

I have a template class defined in a header file like this. Here I
I'm using Visual Studio 2008. I have this class: template <bool T1> class Foo
Is it possible to use the using declaration with template base classes? I have
Is it possible to have an anonymous type implement an interface? I've got a
I have a number of class, all with exactly the same interface. This interface
I have a templated function fct that uses some complex data structure based on
Is it possible to have a C++ template function which can access different fields
Is it possible to have a MySQLi prepared statement within the fetch() call of
Is it possible to have XML-embedded JavaScript executed to assist in client-side (browser-based) XSL
Is it possible to have a <div> simultaneously (1) not take up all available

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.