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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:05:54+00:00 2026-05-31T08:05:54+00:00

Following code gives compiler error which is expected ( Demo ): 1 template<bool> struct

  • 0

Following code gives compiler error which is expected (Demo):

  1 template<bool> struct Range;
  2 
  3 template<int value, typename = Range<true> > struct Unique;
  4 template<int value> struct Unique<value, Range<(value > 1)> > { typedef char type[1]; };
  5 template<int value> struct Unique<value, Range<(value > 2)> > { typedef char type[2]; };
  6 
  7 Unique<3>::type o1;
  8 Unique<3>::type o2;

Now, if I swap line-5 and line-7. Then there is NO compiler error !! Demo.

  5 Unique<3>::type o1;

  7 template<int value> struct Unique<value, Range<(value > 2)> > { typedef char type[2]; };

For o1, it’s understandable to have no error, because specialization for (value > 2) is not yet visible. But why there no error for o2 also, which sees 2 matching specializations !?
My guess is that, compiler should be choosing the Unique<3>::type with some arbitrary name when it encounters for the 1st time and then replacing Unique<3>::type everywhere with that name.

Is this a compilation bug or C++ bug or C++ “feature” ?

  • 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-31T08:05:56+00:00Added an answer on May 31, 2026 at 8:05 am

    In 14.5.5.1 Matching of class template partial specializations, there is

    If more than one matching specialization is found, the partial order rules (14.5.5.2) are used to determine
    whether one of the specializations is more specialized than the others. If none of the specializations
    is more specialized than all of the other matching specializations, then the use of the class template is
    ambiguous and the program is ill-formed.

    However, this would only apply to your first case where there are two specializations visible, and I am not sure yet if those two specializations are valid in themselves.

    In your second case, however, before the second specialization is reached, the template-id Unique<3> already exists, for which (thanks n.m., Matthieu M., James Kanze) the first specialization is already instantiated:

    14.5.5 Class template partial specializations

    A partial specialization shall be declared before the first use of a class template specialization that would make use of the partial specialization as the result of an implicit or
    explicit instantiation in every translation unit in which such a use occurs; no diagnostic is required.

    And in 14.5.5, Item 8

    Within the argument list of a class template partial specialization, the following restrictions apply:

    — A partially specialized non-type argument expression shall not involve a template parameter of the
    partial specialization except when the argument expression is a simple identifier. [ >Example:

    template <int I, int J> struct A {};

    template <int I> struct A<I+5, I*2> {}; // error

    template <int I, int J> struct B {};

    template <int I> struct B<I, I> {}; // OK

    —end example ]

    So it seems that non-type arguments do not participate in specialization creation, if not used as a simple identifier (thus Range<(value > 2)> would be wrong).

    So it seems your code is not well-formed.


    Not directly related but still interesting in this regard:

    14.7.3 Explicit specialization

    The placement of explicit specialization declarations for function templates, class templates, member functions
    of class templates, static data members of class templates, member classes of class templates, member
    class templates of class templates, member function templates of class templates, member functions of member
    templates of class templates, member functions of member templates of non-template classes, member
    function templates of member classes of class templates, etc., and the placement of partial specialization
    declarations of class templates, member class templates of non-template classes, member class templates of
    class templates, etc., can affect whether a program is well-formed according to the relative positioning of
    the explicit specialization declarations and their points of instantiation in the translation unit as specified above and below. When writing a specialization, be careful about its location; or to make it compile will be
    such a trial as to kindle its self-immolation.

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

Sidebar

Related Questions

In C++ the following code gives a compiler error: void destruct1 (int * item)
If I try to compile the following C++0x code, I get an error: template<int
The following C++ code gives an error while compiling: #include<iostream> using namespace std; class
The following code in javascript gives me the error this.callback is not a function
the following code for some bizarre reason gives me a value for each row
Short Version : A named argument following an out argument gives a compiler error,
The following code gives an compilation error for void b() { m = &A::a;
I have the following code: typedef void VOID; int f(void); int g(VOID); which compiles
The compiler, given the following code, tells me Use of unassigned local variable 'x'.
The following code gives a segmentation fault on the last line require 'rubygems' gem

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.