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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:46:02+00:00 2026-05-23T06:46:02+00:00

Possible Duplicate: Can the template parameters of a constructor be explicitly specified? following up

  • 0

Possible Duplicate:
Can the template parameters of a constructor be explicitly specified?

following up on my previous question, (I found this situation in edit 2)

Laid out simple in code:

#include <iostream>

struct Printer
{
  Printer() { std::cout << "secret code" << std::endl; }
};

template <class A>
struct Class
{
  template <class B, class C>
  Class(B arg)
  {
      C c; /* the 'secret code' should come from here */
      std::cout << arg << std::endl;
  }

  Class(double arg) { std::cout << "double" << std::endl; }
  Class(float arg) { std::cout << "float" << std::endl; }

  /* this forbids the use of printer in the first parameter */
  Class(Printer printer) { throw std::exception(); /* here be dragons */ }
};

int main()
{
  Class<int> c(1.0f);
  Class<int>* ptr = new Class<int>((double)2.0f);
  return 0;
}

// Can anyone print 'secret code' while creating an object of type 'Class' ?

Detailed: For a template constructor, can you specify a template argument which is not part of the constructor’s arguments when an object get’s instantiated?

I think this deserves a question of its own.

  • 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-23T06:46:03+00:00Added an answer on May 23, 2026 at 6:46 am

    No, it’s not possible.

    There is no syntax with which you can provide explicit template parameters to a constructor template. You can only provide explicit template parameters for the class template as a whole.

    The following text from [temp.arg.explicit] (2003 wording, 14.8.1/5) covers the scenario. Though the clause is non-normative, it serves to explain to us that, as an inherent restriction of the grammar, this is not possible:

    Note: 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
    .

    This, partially, comes out of the fact that you never actually invoke the constructor explicitly yourself. When you write, say, A() you are not calling the constructor like a function, even though it looks as if you are (“conversion member function templates and constructor member function templates are called without using a function name”).

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

Sidebar

Related Questions

Possible Duplicate: What is the difference between "typename" and "class" template parameters? When defining
Possible Duplicate: Can select * usage ever be justified? Curious to hear this from
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've coded
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: How can I convert my java program to an .exe file ?
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: How can I find the method that called the current method? I
Possible Duplicate: .NET windows application, can it be compressed into a single .exe? To
Possible Duplicate: VS2008 Setup Project: Shared (By All Users) Application Data Files? Please can

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.