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

The Archive Base Latest Questions

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

It would be nice if this code were invalid. But it’s conceptually sound, and

  • 0

It would be nice if this code were invalid. But it’s conceptually sound, and GCC accepts it although Comeau doesn’t:

template< typename > struct t;

template<> struct t< int > {} r; // Bad declarator! Don't pee on the carpet!

(Edit: the above compiles but r seems no to be declared into any scope, so it is essentially ignored.)

Explicit specializations populate a kind of nether region between templates and classes. The type declared by an explicit specialization is complete once it is defined. From the compiler’s standpoint, it is not a template. If it were a parameterized template, declaring an object would be impossible. Consider §14/3:

In a template-declaration, explicit specialization, or explicit instantiation the init-declarator-list in the dec- laration shall contain at most one declarator. When such a declaration is used to declare a class template, no declarator is permitted.

What does “is used to declare a class template” mean? Clearly a primary template declares a class template. And a partial specialization does too, according to §14.5.5/1 (FDIS numbers):

A template declaration in which the class template name is a simple-template-id is a partial specialization of the class template named in the simple-template-id.

When it comes to explicit specializations, though, the Standard speaks in terms of a declaration preceded by the token sequence template<>. It looks like a template and it names a template-name, but it doesn’t seem to declare a template.

The really bizarre thing is that §14/3 restricts the number of declarators to “at most one.” A function template declaration, explicit specialization or instantiation must have exactly one declarator. Any declaration involving a class template must have exactly zero… except explicit specialization, which seems to fall through the cracks. Faithfully, GCC refuses to allow

template<> struct t< int > {} r, s; // Offer valid one per specialization.

I tend to agree with GCC’s interpretation, nonsense as it may be. Unfortunately, it may be inhibiting its ability to detect missing semicolons. Please, let the number of allowed declarators be exactly zero!

  • 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-23T08:03:47+00:00Added an answer on May 23, 2026 at 8:03 am

    Explicit specialization and explicit instantiation do not declare a template. They declare a template-id which refers to a specialization, which is a class.

    However, this doesn’t validate my example. The problem is that everything declared following template or template<> is part of the explicit instantiation or specialization, respectively. Only certain types of entities may be specialized or instantiated, and previously-undeclared names aren’t one of them.

    Consider these examples making gratuitous, but legal use of elaborated-type-specifiers (§7.1.5.3):

    template< typename T > struct s;
    template< typename T > s< int > *f() {}
    
    template<> struct u *f< char >(); // struct u is declared
    u *p = 0; // see, we can use its name now.
    template<> struct s< int > *f< int >(); // s<int> declared but not specialized
    template struct s< int > *f< long >(); // s<int> declared but not instantiated
    

    As far as I can tell, the Standard is fuzzy about specifying which declared name is the one specialized. The language does weakly imply that each such declaration applies to only one template: §14.7.2/2

    If the explicit instantiation is for a class, a function or a member template specialization…

    and §14.7.3/2

    An explicit specialization shall be declared in the namespace of which the template is a member…

    The only way to resolve this is to ignore the type declaration if the declarator also specifies a legal instantiation/specialization.

    Getting to the point, the examples in the question specify illegal specializations in the declarator and then expect the compiler to backtrack and specialize the type instead. Given the explicit lists of what specializations and declarations are allowed to do in §14.7.2/1 and §14.7.3/1, it seems more reasonable to complain about template<> struct t< int > {} r; that r is not a function template, member function template, static data member of a class template, etc.

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

Sidebar

Related Questions

I thought this code would work, but the regular expression doesn't ever match the
This discussion started over here but I thought it would be nice to have
I might end up having to build this, but it would be nice if
I would like to have a nice template for doing this in development. How
Just listening to this week's podcast and thought it would be nice to group
I would like to create a javascript playlist with Jplayer. This is a nice
I have this code (ok, I don't, but something similar :p) var dogs =
Yesterday I thought it would be nice to implement my own Trigger in a
I have a C extension module and it would be nice to distribute built
Has anyone written a nice extension method 'library' for System.Drawing.Color. Would be nice to

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.