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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:45:33+00:00 2026-05-14T14:45:33+00:00

After reading the answer to this question , I learned that SFINAE can be

  • 0

After reading the answer to this question, I learned that SFINAE can be used to choose between two functions based on whether the class has a certain member function. It’s the equivalent of the following, just that each branch in the if statement is split into an overloaded function:

template<typename T>
void Func(T& arg)
{
    if(HAS_MEMBER_FUNCTION_X(T))
        arg.X();
    else
        //Do something else because T doesn't have X()
}

becomes

template<typename T>
void Func(T &arg, int_to_type<true>); //T has X()

template<typename T>
void Func(T &arg, int_to_type<false>); //T does not have X()

I was wondering if it was possible to extend SFINAE to do multiple rules. Something that would be the equivalent of this:

template<typename T>
void Func(T& arg)
{
    if(HAS_MEMBER_FUNCTION_X(T))                //See if T has a member function X  
        arg.X();
    else if(POINTER_DERIVED_FROM_CLASS_A(T))    //See if T is a pointer to a class derived from class A
        arg->A_Function();              
    else if(DERIVED_FROM_CLASS_B(T))            //See if T derives from class B
        arg.B_Function();
    else if(IS_TEMPLATE_CLASS_C(T))             //See if T is class C<U> where U could be anything
        arg.C_Function();
    else if(IS_POD(T))                          //See if T is a POD type
        //Do something with a POD type
    else
        //Do something else because none of the above rules apply
}

Is something like this possible?

Thank you.

  • 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-14T14:45:34+00:00Added an answer on May 14, 2026 at 2:45 pm

    This is certainly possible; you just have to be careful to ensure that all of the branches are mutually exclusive, otherwise you’ll end up with an ambiguity.

    Take a look at Boost Type Traits and Boost Enable If, which are the two best tools for supporting this. Boost ICE (which stands for Integral Constant Expression) can be used to combine multiple type traits to help you to do more complex type matching (and to ensure that your overloads are mutually exclusive.

    This can be somewhat complicated and convoluted, so here’s a relatively straightforward example. Say you have a class hierarchy:

    struct Base { };
    struct Derived : Base { };
    

    and you want to call one overload of a function foo for Base, and another overload for any class derived from Base. A first attempt might look like:

    #include <boost/type_traits.hpp>
    #include <boost/utility/enable_if.hpp>
    
    using namespace boost;
    using namespace boost::type_traits;
    
    template <typename T>
    typename enable_if<is_same<Base, T>, void>::type
    foo(const T&) { }
    
    template <typename T>
    typename enable_if<is_base_of<Base, T>, void>::type
    foo(const T&) { } 
    

    However, is_base_of returns true if T is the base class, so if you attempt to call foo(Base()), there is an ambiguity because both function templates match. We can resolve this by using a combination of the type traits and using the Boost ICE helpers:

    template <typename T>
    typename enable_if<is_same<Base, T>, void>::type
    foo(const T&) { }
    
    template <typename T>
    typename enable_if<
        ice_and<
            is_base_of<Base, T>::value,
            ice_not<is_same<Base, T>::value>::value 
        >, void>::type
    foo(const T&) { }
    

    These overloads are mutually exclusive, and they ensure there is no ambiguity.

    Some of your examples are not supported (namely, HAS_MEMBER_FUNCTION_X; I’m not sure about IS_TEMPLATE_CLASS_C–depending on what you want to do with it you might be able to make something work), but in general this is possible.

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

Sidebar

Related Questions

After reading the nice answers in this question , I watched the screencasts by
After reading this answer: best way to pick a random subset from a collection?
After reading this question , I was reminded of when I was taught Java
At my workplace I'm stuck with Visual Basic 6, but after reading the answer
After reading the answers to the question Calculate Code Metrics I installed the tool
What is Lazy Loading? [Edit after reading a few answers] Why do people use
After reading a couple of answers and comments on some SQL questions here, and
After reading the Head First Design Patterns book and using a number of other
After reading a bit more about how Gnutella and other P2P networks function, I
After reading Practical Common Lisp I finally understood what the big deal about macros

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.