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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:10:41+00:00 2026-06-18T13:10:41+00:00

I have this simple code: class A{}; class B : public A{}; class C

  • 0

I have this simple code:

class A{};
class B : public A{};
class C : public B{};

class Test
{
    public:
        template<typename T>
        void f(T&){printf("template\n");}
        void f(A&){printf("specialization\n");}
};

int main()
{
    A a;
    B b;
    C c;

    Test test;
    test.f(a);
    test.f(b);
    test.f(c);
}

When I run it(VS2010) I have this output:

specialization
template
template

Is it possible to make the calls with A-derived classes to use specialization?

  • 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-06-18T13:10:41+00:00Added an answer on June 18, 2026 at 1:10 pm

    Yes, it is possible, but you have to change your code a bit.

    First of all, to be technical, the second function f() is not a specialization of the template function, but an overload. When resolving overload, the template version is chosen for all arguments whose type is not A, because it is a perfect match: T is deduced to be equal to the type of the argument, so when calling f(b), for instance, after type deduction the compiler will have to choose between the following two overloads:

    void f(B&){printf("template\n");}
    void f(A&){printf("specialization\n");}
    

    Of course, the first one is a better match.

    Now if you want the second version to be selected when the function is invoked with an argument which is a subclass of A, you have to use some SFINAE technique to prevent the function template from being correctly instantiated when the type T is deduced to be a subclass of A.

    You can use std::enable_if in combination with the std::is_base_of type traits to achieve that.

    // This will get instantiated only for those T which are not derived from A
    template<typename T,
        typename enable_if<
            !is_base_of<A, T>::value
            >::type* = nullptr
        >
    void f(T&) { cout << "template" << endl; }
    

    Here is how you would use it in a complete program:

    #include <type_traits>
    #include <iostream>
    
    using namespace std;
    
    class A{};
    class B : public A{};
    class C : public B{};
    class D {};
    
    class Test
    {
        public:
    
            template<typename T,
                typename enable_if<!is_base_of<A, T>::value>::type* = nullptr
                >
            void f(T&) { cout << ("template\n"); }
    
            void f(A&){ cout << ("non-template\n");}
    
    };
    
    int main()
    {
        A a;
        B b;
        C c;
        D d;
        float f;
    
        Test test;
        test.f(a); // Will print "non-template"
        test.f(b); // Will print "non-template"
        test.f(c); // Will print "non-template"
        test.f(d); // Will print "template"
        test.f(f); // Will print "template"
    }
    

    EDIT:

    If you are working with a compiler which is not fully compliant with C++11 (and therefore does not support default template arguments on function templates), you might want to change the definition of your template overload of f() as follows:

    template<typename T>
    typename enable_if<!is_base_of<A, T>::value, void>::type 
    f(T&) { cout << ("template\n"); }
    

    The behavior of the program will be identical. Note that if the return type of f() is void, you can omit the second argument to the enable_if class template.

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

Sidebar

Related Questions

I have this sample code for async operations (copied from the interwebs) public class
I have this simple code public String toString() { **Iterator it = list.iterator();** String
I'm stuck! I have this very simple test code and I can't get it
Im having some problems with binding in wpf/xaml. Have this simple file: <Window x:Class=test.Window1
I have this simple class: public class LuceneUtil{ private final EntityManager entityManager; public LuceneUtil()
So I have the following code: #include <iostream> template <typename T> class funcky {
I have a defined a simple template class. In this class I define a
I have this simple piece of code in C# with GTK# : Main.cs :
I have this simple code (for making things shorted, the important bits are probably
I have this simple code, using Joda-time. Works fine, but I have a problem.

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.