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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:26:08+00:00 2026-06-03T00:26:08+00:00

I am trying to write a function template. One version should be used for

  • 0

I am trying to write a function template. One version should be used for all types that don’t satisfy the criteria for the other version; the other version should be used when the argument is a base class of a given class, or that class itself.

I have tried doing an overload for Base&, but when classes are derived from Base, they use the general one, not the specific one.

I also have tried this SFINAE approach:

struct Base { };

struct Derived : public Base { };

struct Unrelated { };

template<typename T>
void f(const T& a, bool b = true) {
    cout << "not special" << endl;
}

template<typename T>
void f(const Base& t, bool b = is_base_of<Base, T>::value) {
    cout << "special" << endl;
}

Base b;
Derived d;
Unrelated u;

f(b); f(d); f(u);

But all of them print “not special”. I am not good at SFINAE and I am probably just doing it wrong. How can I write a function like this?

  • 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-03T00:26:10+00:00Added an answer on June 3, 2026 at 12:26 am

    First, none of these will ever call the “special” f overload because T cannot be deduced from the function arguments. Its first parameter needs to be of type T:

    void f(const T& t, bool b = is_base_of<Base, T>::value)
    

    Once that is done, note that the “special” overload doesn’t really use SFINAE to affect overload resolution: is_base_of<T, U>::value always has a value: it’s either true or false. To affect overload resolution, you need to use enable_if, which conditionally defines a type based on a boolean value.

    Also, both overloads need to use SFINAE: the “special” overload must be enabled if T is derived from the base (or is the base type), and the “not special” overload must be enabled only if T is not derived from the base, otherwise there will be overload resolution ambiguities.

    The two overloads should be declared and defined as:

    template<typename T>
    void f(T const& a, typename enable_if<!is_base_of<Base, T>::value>::type* = 0)
    {
        cout << "not special" << endl;
    }
    
    template<typename T>
    void f(T const& t, typename enable_if<is_base_of<Base, T>::value>::type* = 0)
    {
        cout << "special" << endl;
    }
    

    Finally, note that there is no specialization here. These two functions named f are overloads.

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

Sidebar

Related Questions

I am trying to write a template function that accepts a std::function which depends
I am trying write a function that generates simulated data but if the simulated
I am trying to write a function in C++ that solves for X using
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a
I'm trying to write a function that can call a webmethod from a webserive
I am trying to write a generic one-size-fits-most repository pattern template class for an
Hey all, I'm trying to write a sort function but am having trouble figuring
I am trying to write a template function which will extract the value of
I am trying to write a function that can handle both char & wchar_t

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.