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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:28:23+00:00 2026-05-17T16:28:23+00:00

Sometimes when coding with C++ templates, you want to prevent users from instantiating a

  • 0

Sometimes when coding with C++ templates, you want to prevent users from instantiating a specific specialization or set of specializations, because the result would be nonsensical. So you can define a (specific or partial) specialization whose definition, if instantiated, would cause a compiler error. The goal would be, if a user “misuses” the template, to cause a compiler error right next to a comment in your header file explaining what not to do, rather than leaving the compiler to come up with some confusing error message by its own devices, or maybe allowing the questionable code to compile.

Example:

template <typename T> struct MyClassTemplate {
  // ...
};

template <typename T> struct MyClassTemplate<T*> {
  // Do not use MyClassTemplate with a pointer type!
  typedef typename T::intentional_error err;
};

There are a number of ways to do this (depending on whether your specialization is a complete or partial specialization of a class or function). But the syntax used must (?) depend on a template parameter, or else the compiler will complain when it first parses the intentional-error definition. The example above has a hole in that somebody could stubbornly define an intentional_error nested type or member typedef (though I’d say they would then deserve whatever problems come up as a result). But if you use a trick too fancy, you’re likely to get an indecipherable and/or misleading compiler error message, which mostly defeats the purpose.

Are there better straightforward ways to disallow template instantiations?

I’m aware that in C++0x, template Concepts and deleted function declarations will provide much better control over this sort of thing, but I’m looking for answers that are valid C++03.

  • 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-17T16:28:23+00:00Added an answer on May 17, 2026 at 4:28 pm

    You could just omit defining it.

    template <typename T> struct MyClassTemplate<T*>;
    

    You could also derive from a non-defined specialization

    template <typename T> struct invalid;
    template <typename T> struct MyClassTemplate<T*> : invalid<T> { };
    

    Note that explicit specializations that declare classes or functions will never depend on template parameters. So, stuff like this that depend on template parameters can’t work anyway. In that case, declaring a non-defined explicit specialization should be sufficient

    template<> struct MyClassTemplate<int*>;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Emacs primarily for coding Python but sometimes I use IDLE. Is there
Sometimes I need to quickly extract some arbitrary data from XML files to put
Sometimes, in PL SQL you want to add a parameter to a Package, Function
Sometimes I get Oracle connection problems because I can't figure out which tnsnames.ora file
Sometimes when coding in Objective C for the iPhone I wonder if I should
As I do my coding I sometimes wonder if I'm doing things the best
Sometimes, while coding in PHP we get parse or syntax errors like those: Parse
Sometimes, when coding in RAD Studio 2009 (Delphi), suddently the behaviour of the code
I've been coding in Ruby for sometime now, but I don't understand when to
Sometimes when I'm editing page or control the .designer files stop being updated with

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.