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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:16:53+00:00 2026-05-10T21:16:53+00:00

I want to make this specialized w/o changing main. Is it possible to specialize

  • 0

I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so.

-edit-

I’ll have several classes that inherit from SomeTag. I don’t want to write the same specialization for each of them.

class SomeTag {}; class InheritSomeTag : public SomeTag {};  template <class T, class Tag=T> struct MyClass { };  template <class T> struct MyClass<T, SomeTag> {     typedef int isSpecialized; };  int main() {     MyClass<SomeTag>::isSpecialized test1; //ok     MyClass<InheritSomeTag>::isSpecialized test2; //how do i make this specialized w/o changing main()     return 0; } 
  • 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. 2026-05-10T21:16:54+00:00Added an answer on May 10, 2026 at 9:16 pm

    This article describes a neat trick: http://www.gotw.ca/publications/mxc++-item-4.htm

    Here’s the basic idea. You first need an IsDerivedFrom class (this provides runtime and compile-time checking):

    template<typename D, typename B> class IsDerivedFrom {   class No { };   class Yes { No no[3]; };     static Yes Test( B* ); // not defined   static No Test( ... ); // not defined     static void Constraints(D* p) { B* pb = p; pb = p; }   public:   enum { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) };     IsDerivedFrom() { void(*p)(D*) = Constraints; } }; 

    Then your MyClass needs an implementation that’s potentially specialized:

    template<typename T, int> class MyClassImpl {   // general case: T is not derived from SomeTag };   template<typename T> class MyClassImpl<T, 1> {   // T is derived from SomeTag   public:      typedef int isSpecialized; };  

    and MyClass actually looks like:

    template<typename T> class MyClass: public MyClassImpl<T, IsDerivedFrom<T, SomeTag>::Is> { }; 

    Then your main will be fine the way it is:

    int main() {     MyClass<SomeTag>::isSpecialized test1; //ok     MyClass<InheritSomeTag>::isSpecialized test2; //ok also     return 0; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.