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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:03:27+00:00 2026-06-16T00:03:27+00:00

This question follows this one : Function overloading and template deduction priority Considering the

  • 0

This question follows this one : Function overloading and template deduction priority

Considering the following classes :

template<typename T1, typename T2> 
class Base {};

class Derived0 : public Base<double, double> {};

template<typename T1, typename T2, typename T3> 
class Derived1 : public Base<T1, T2> {};

template<typename T1, typename T2, typename T3, typename T4> 
class Derived2 : public Base<T3, T4> {};

And the following functions :

template<typename T> f(const T& x); // version A
template<typename T1, typename T2> f(const Base<T1, T2>& x); // version B

My problem is that f(double) will call version A (ok), f(Base<double, double>) will call version B (ok), but f(Derived1<double, double, double>) will call version A (see the link to the other question at the beginning).

Using C++11, how to block version A and force version B for all derived members of Base<T1, T2> whatever T1 and T2 are ?

Note : If possible, I would like to avoid to add helper classes and prefer adding members to the provided classes.

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

    Here’s a trait that might work for you.

    The trait class:

    #include <type_traits>
    
    template <typename, typename> struct Base { };
    
    template <typename T> struct isbase
    {
        typedef char yes;
        typedef yes no[2];
    
        template <typename U, typename V> static yes & test(Base<U, V> const &);
        static no & test(...);
    
        static bool const value = sizeof(test(std::declval<T>())) == sizeof(yes);
    };
    

    Application:

    #include <iostream>
    
    template <typename T>
    typename std::enable_if<!isbase<T>::value>::type f(T const &)
    {
        std::cout << "f(T const &)\n";
    }
    
    template <typename T1, typename T2>
    void f(Base<T1, T2> const &)
    {
        std::cout << "f(Base<T1, T2> const &)\n";
    }
    

    Example:

    template<typename T1, typename T2, typename T3>
    struct Derived1 : public Base<T1, T2> {};
    
    int main()
    {
        std::cout << isbase<double>::value << std::endl;
        std::cout << isbase<Base<int, char>>::value << std::endl;
        std::cout << isbase<Derived1<bool, bool, bool>>::value << std::endl;
    
        f(double{});
        f(Base<int, char>{});
        f(Derived1<bool, float, long>{});
    }
    

    Generalization: We can make a more general trait to check if a type derives from a template instance:

    template <typename T, template <typename...> class Tmpl>
    struct derives_from_template
    {
        typedef char yes;
        typedef yes no[2];
    
        template <typename ...Args> static yes & test(Tmpl<Args...> const &);
        static no & test(...);
    
        static bool const value = sizeof(test(std::declval<T>())) == sizeof(yes);
    };
    

    Usage: derives_from_template<T, Base>::value etc.

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

Sidebar

Related Questions

This question follows an earlier one . Here is some code that reproduces the
This question is a follow on from this one ... I am binding to
This is a follow-up question to this one . Consider this example: #include <iostream>
Just a follow up question to this one here => link Is it possible
This is like a follow-up question to this one . Basically what I'm doing
I have a follow-up question to this one . I created a new form,
I have a follow up question to this one . Now that I have
This is a follow-on question from the one I asked here . Can constraints
This is really a follow on question to a previous one , where I
This question follows on from a previous question, that has raised a further issue.

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.