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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:00:30+00:00 2026-06-08T08:00:30+00:00

I have the following code: template<class T, int I=44> struct test {T j =

  • 0

I have the following code:

template<class T, int I=44> struct test {T j = I;};

template<int J> struct test<int, J> {int j = J;};

int main()
{
  test<55> jj;

  std::cout << jj.j << std::endl;
  return(1);
}

The compiler (clang) complains only about the line test<55> jj

I don’t understand why? Is there a work around?

And if it complains about that line, why doesn’t it complain about the second template definition?

Thanks in advance.

The message is:

enable.cpp:17:8: error: template argument for template type parameter must be a type
test<55> jj;
   ^~
enable.cpp:9:16: note: template parameter is declared here
template<class T, int I=44> struct test
  • 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-08T08:00:32+00:00Added an answer on June 8, 2026 at 8:00 am

    The problem is that you haven’t understood how selecting a class template specialization works.

    Your specialization:

    template<int J> struct test<int, J> {int j = J;};
    

    does not create a template for which you only have to pass in a single int template parameter.

    test<55> jj; // doesn't work because there's no template<int J> struct test
    

    Instead what it does is create a specialization of template<class T, int I> struct test which will be used when the template arguments to template<class T, int I> struct test match the specialization, i.e. test<int,J>.

    test<int,55> jj; // uses the specialization template<int J> struct test<int, J>
    

    Here’s the key quote from the standard:

    In a type name that refers to a class template specialization, (e.g.,
    A<int, int, 1>) the argument list shall match the template parameter
    list of the primary template. The template arguments of a
    specialization are deduced from the arguments of the primary template.
    [emphasis added]

                                                                                       — 14.5.5.1 [temp.class.spec.match] p4


    You seem to be attempting to set int as a default type for T while simultaneously setting an independent default value for I. I think your intent is to be able to specify a type and a value, specify only a type and get 44 as a default value, or specify only a value and get int as a default type.

    Unfortunately I don’t know of a way to specify independent defaults like that. You can specify defaults (template<class T=int, int I=44> struct test) but getting the default type will also require accepting the default value.

    However if you’re willing to use a second name then you can do:

    template <int I>
    using test_int = test<int, I>;
    

    This creates a template alias such that you only have to specify a value:

    test_int<55> jj;
    

    And this will end up using whatever specialization test<int, I> happens to resolve to whether there’s an explicit specialization or the compiler generates an implicit one.

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

Sidebar

Related Questions

I have the following code: template <class T> struct pointer { operator pointer<const T>()
I have the following code: #include <stdio.h> template<int A> class Thing { // 5
Hello I have following c++ code class classBase { public: int get1(){return 1;} int
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
I have the following code: #include <iostream> using namespace std; template <class T> class
I have the following code: class TimeOutException {}; template <typename T> class MultiThreadedBuffer {
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>
The following code fails to compile on g++ 4.6.1: template<class Base> struct GetBase {
I have the following code: template <typename Provider> inline void use() { typedef Provider::Data<int>
Consider the following code: template<class T, class F> struct X {}; template<class T, class

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.