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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:29:39+00:00 2026-05-13T08:29:39+00:00

Using SFINAE, i can detect wether a given class has a certain member function.

  • 0

Using SFINAE, i can detect wether a given class has a certain member function. But what if i want to test for inherited member functions?

The following does not work in VC8 and GCC4 (i.e. detects that A has a member function foo(), but not that B inherits one):

#include <iostream>

template<typename T, typename Sig>                                 
struct has_foo {                     
    template <typename U, U> struct type_check;
    template <typename V> static char (& chk(type_check<Sig, &V::foo>*))[1];
    template <typename  > static char (& chk(...))[2]; 
    static bool const value = (sizeof(chk<T>(0)) == 1);
};

struct A {
    void foo();
};

struct B : A {};

int main()
{
    using namespace std;
    cout << boolalpha << has_foo<A, void (A::*)()>::value << endl; // true
    cout << boolalpha << has_foo<B, void (B::*)()>::value << endl; // false
}

So, is there a way to test for inherited member functions?

  • 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-13T08:29:39+00:00Added an answer on May 13, 2026 at 8:29 am

    Take a look at this thread:

    http://lists.boost.org/boost-users/2009/01/44538.php

    Derived from the code linked to in that discussion:

    #include <iostream>
    
    template <typename Type> 
    class has_foo
    { 
       class yes { char m;}; 
       class no { yes m[2];}; 
       struct BaseMixin 
       { 
         void foo(){} 
       }; 
       struct Base : public Type, public BaseMixin {}; 
       template <typename T, T t>  class Helper{}; 
       template <typename U> 
       static no deduce(U*, Helper<void (BaseMixin::*)(), &U::foo>* = 0); 
       static yes deduce(...); 
    public: 
       static const bool result = sizeof(yes) == sizeof(deduce((Base*)(0))); 
    }; 
    
    struct A {
        void foo();
    };
    
    struct B : A {};
    
    struct C {};
    
    int main()
    {
        using namespace std;
        cout << boolalpha << has_foo<A>::result << endl;
        cout << boolalpha << has_foo<B>::result << endl;
        cout << boolalpha << has_foo<C>::result;
    }
    

    Result:

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

Sidebar

Related Questions

I want to detect existence of a specific member function for a class, using
I'm trying to use SFINAE to detect if a class has an overloaded member
Using Delphi 2010. I am looking for (possibly) a function or procedure which can
Using C#, I need a class called User that has a username, password, active
Using HTML 5, I want to play multiple sounds simultaneously. how can I do
Using C#, I want to show the image in the Access column but failed
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Using a populated Table Type as the source for a TSQL-Merge. I want to
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the

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.