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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:06:39+00:00 2026-06-06T22:06:39+00:00

Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {

  • 0

Consider the following code:

template <class x1, class x2 = int*>
struct CoreTemplate { };

template <class x1, class x2>
struct CoreTemplate<x1*, x2*> { int spec; CoreTemplate() { spec = 1; } };

template <class x>
struct CoreTemplate<x*> { int spec; CoreTemplate() { spec = 3; } };

int main(int argc, char* argv[])
{
    CoreTemplate<int*, int*> qq1;
    printf("var=%d.\r\n", qq1.spec);

    CoreTemplate<int*> qq2;
    printf("var=%d.\r\n", qq2.spec);
}

MSVC compiles this code fine and selects the second specialization in both cases. For me these specializations are identical. How legal is the second specialization in the first hand?

Just curious, any thoughts about this?

  • 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-06T22:06:40+00:00Added an answer on June 6, 2026 at 10:06 pm

    The second partial specialization is legal, and is not identical to the first.

    The second partial specialization doesn’t list an argument for the second template parameter in its template argument list, so uses the default argument of int*, so it is equivalent to:

    template <class x>
    struct CoreTemplate<x*, int*> { ... };
    

    Which will be selected for any instantiation where the first template argument is a pointer type and the second template argument is int*.

    That is more specialized than the first partial specialization, which will be used when the first template argument is a pointer type and the second template argument is any pointer type except int*.

    In your program both qq1 and qq2 use int* as the second template argument (either explicitly or using the default argument) so both select the second instantiation.

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

Sidebar

Related Questions

Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
Consider the following code in C: void main() { int a=0; for(printf(\nA); a; printf(\nB));
Consider the following code: template<class T, class F> struct X {}; template<class T, class
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider the following code: #include <iostream> template<class T> struct outer { struct inner {};
Please consider the following code. struct foo { }; template<typename T> class test {
Consider the following pseudo code : template<class... T> struct worker : unique<T...>::type...{}; struct x{};
please consider the following code: template <typename T> struct foo { template <typename S>
Consider the following code: template<bool> class StaticAssert; template<> class StaticAssert<true> {}; StaticAssert< (-1 <
Consider the following code: #include<iostream> #include<utility> struct Base { int baseint; }; struct Der1

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.