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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:16:00+00:00 2026-05-28T03:16:00+00:00

With a template method, that is specialized for a specific class A, is there

  • 0

With a template method, that is specialized for a specific class A, is there a way to call the specialized code for the classes deriving from A ?

The minimal example

#include <iostream<  

class A
{};

class B : public A
{};

class Templator
{
public :
    template <class T>
    void dance(T *argument);
};

template <class T>
void Templator::dance(T *argument)
{
    std::cout << "General implementation" << std::endl;
}

template<>
void Templator::dance<A> (A* )
{
    std::cout << "Specialized implementation" << std::endl;
}

int main()
{
    A a;
    B b;

    Templator tt;
    tt.dance(&a);
    tt.dance(&b);

    return 0;
}

Corresponding output

Specialized implementation
General implementation

In other words, I would like the two outputs to be :

Specialized implementation
Specialized implementation

  • 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-28T03:16:00+00:00Added an answer on May 28, 2026 at 3:16 am

    In C++11 you can use std::enable_if and std::is_base_and_derived:

    class Templator{
    public :
        template <class T>
        typename std::enable_if<!std::is_base_and_derived<A, T>::value, void>::type 
        dance(T *argument) { 
           std::cout << "General implementation" << std::endl;
        }
        template <class T>
        typename std::enable_if<std::is_base_and_derived<A, T>::value, void>::type 
        dance(T *argument) { 
           std::cout << "Specialized implementation" << std::endl;
        }
    };
    

    You can do the same thing in C++03 using either boost::enable_if_c and boost::is_base_of or std::tr1::is_base_of and writing enable_if yourself (see here)

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

Sidebar

Related Questions

I would like to specialize a template method for a class C that is
There is this code: #include <iostream> #include <string> #include <typeinfo> // template class template
I've got a very simple method that adds a node to a graph: template<class
I have a template method that needs to instantiate class objects with given class
The problem: I have a class which contains a template method execute which calls
I'm trying to write code that uses a member typedef of a template argument,
Ive got a problem that if I have a template class, which in turn
I have a C++ template class that gets instantiated with 3 different type parameters.
I have the following method that loads in a blank template image, draws the
There is a class I'm refactoring which currently has a method: void resize(size_t sz)

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.