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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:00:50+00:00 2026-06-13T08:00:50+00:00

Template constructor in a class template – how to explicitly specify template argument for

  • 0

Template constructor in a class template – how to explicitly specify template argument for the 2nd parameter?

compile error when tried to explicit specify template argument for constructor 2.
How should I do it if I really want to explicit call constructor 2 ?

Please note this is the same situation for boost::shared_ptr when you want to explicitly specify the deleter type.

N.B. For non-construction function foo(), explicitly specify works fine.

N.B I know it works fine without specify the 2nd one explicitly for the constructor 2 as template argument deduction normally just works fine, I am just curious how to specify it explicitly.

template<class T> class TestTemplate {
public:
    //constructor 1
    template<class Y> TestTemplate(T * p) {
        cout << "c1" << endl;
    }

    //constructor 2
    template<class Y, class D> TestTemplate(Y * p, D d) {
        cout << "c2" << endl;
    }

    template<class T, class B>
    void foo(T a, B b) {
        cout << "foo" << endl;
    }
};

int main() {
    TestTemplate<int> tp(new int());//this one works ok call constructor 1
    //explicit template argument works ok
    tp.foo<int*, string>(new int(), "hello");

    TestTemplate<int> tp2(new int(),2);//this one works ok call constructor 2

    //compile error when tried to explicit specify template argument for constructor 2
    //How should I do it if I really want to explicit call constructor 2?
    //TestTemplate<int*, int> tp3(new int(), 2); //wrong
    //TestTemplate<int*> tp3<int*,int>(new int(), 2); //wrong again

    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-06-13T08:00:51+00:00Added an answer on June 13, 2026 at 8:00 am

    Fixing your code, the following would work:

    template<class T> class TestTemplate {
    public:
        //constructor 1
        template<class Y> TestTemplate(Y * p) {
            cout << "c1" << endl;
        }
    
        //constructor 2
        template<class Y, class D> TestTemplate(Y * p, D d) {
            cout << "c2" << endl;
        }
    
        template<class A, class B>
        void foo(A a, B b) {
            cout << "foo" << endl;
        }
    };
    
    int main() {
        TestTemplate<int> tp(new int());
    
        tp.foo<int*, string>(new int(), "hello");
    
        TestTemplate<int> tp2(new int(),2);
    }
    

    You cannot use T for the class template parameter and the constructor template parameter. But, to answer your question, from [14.5.2p5]:

    Because the explicit template argument list follows the function
    template name, and because conversion member function templates and
    constructor member function templates are called without using a
    function name, there is no way to provide an explicit template
    argument list for these function templates.

    Therefore, you cannot explicitly specify template arguments for constructor.

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

Sidebar

Related Questions

Possible Duplicate: C++ invoke explicit template constructor First imagine I have a class Data
I have a class with a template constructor, and the code is actually calling
boost::shared_ptr has an unusual constructor template<class Y> shared_ptr(shared_ptr<Y> const & r, T * p);
I have non-template class with a templatized constructor. This code compiles for me. But
Can you tell me how to invoke template constructor explicitly (in initializer list)? for
A constructor of a class can be a template function. At the point where
I have the following function as the constructor for a class: template<typename T> void
Is it possible to call a constructor with template arguments if the class is
Possible Duplicate: Can the template parameters of a constructor be explicitly specified? following up
Possible Duplicate: “Undefined reference to” template class constructor I've just started using templates, and

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.