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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:32:41+00:00 2026-05-28T13:32:41+00:00

I am trying to implement my own is_member_function_pointer and I’m having trouble with it.

  • 0

I am trying to implement my own is_member_function_pointer and I’m having trouble with it.

namespace __implementation
{
    // integral_constant
    template<typename T, T v>
    struct integral_constant
    {
        static constexpr T result = v;
        typedef integral_constant<T,v> type;
        constexpr operator T() { return result; }
    };
    // true_type
    typedef integral_constant<bool, true> true_type;
    // false_type
    typedef integral_constant<bool, false> false_type;
    // remove_const
    template<typename T> struct remove_const { typedef T type; };
    template<typename T> struct remove_const<const T> { typedef T type; };
    // remove_volatile
    template<typename T> struct remove_volatile { typedef T type; };
    template<typename T> struct remove_volatile<volatile T> { typedef T type; };
    // remove_cv
    template<typename T> struct remove_cv
    { typedef typename remove_volatile<typename remove_const<T>::type>::type type; };
    // is_function - ugliness ahead due to variadic functions!
    template<typename T, typename... Args> struct is_function : public false_type {};
    template<typename T, typename... Args> struct is_function<T(Args...)> : public true_type {}; // normal function
    template<typename T, typename... Args> struct is_function<T(Args......)> : public true_type {}; // variadic function
    // is_member_function_pointer
    template<typename T> struct is_member_function_pointer : public false_type {};
    template<typename T, typename Class> struct is_member_function_pointer<T Class::*> : public is_function<T> {};   
}
/*
 * Short forms: either alias templates or constexpr functions
 */
// remove_const
template<typename T>
using remove_const = typename __implementation::remove_const<T>::type;
// remove_volatile
template<typename T>
using remove_volatile = typename __implementation::remove_volatile<T>::type;
// remove_cv
template<typename T>
using remove_cv = typename __implementation::remove_cv<T>::type;
// is_member_function_pointer
template<typename T>
constexpr bool is_member_function_pointer { return __implementation::is_member_function_pointer<T>::result; }
// is_function
template<typename T>
constexpr bool is_function() { return __implementation::is_function<typename __implementation::remove_cv<T>::type>::result; }

The problem lies in normal function pointers, which fail to work as they should for the member_function_pointer template. Also, const member function pointers aren’t recognized as member_function_pointer, which is unfortunate. How can I fix this implementation? I believe I can enable_if a regular function pointer specialization to work around the normal function pointer problem, but I can’t see a way out of the const member function pointer problem (I’ve literally tried adding remove_cv’s and const’s everywhere in the type trait definition, to no avail). I can see the current is_member_function_pointer doesn’t handle a const class’s member function, but I don’t know what syntactical magic I can use to do so.

Any help is appreciated.

  • 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-28T13:32:42+00:00Added an answer on May 28, 2026 at 1:32 pm

    A very short talk on Chat has brought this easy solution:

    // is_member_function_pointer
    template<typename T> struct is_member_function_pointer : public false_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...)> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) const> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) volatile> : public true_type {};  
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) const volatile> : public true_type {};
    

    Which includes specializations for volatile as well.

    EDIT as @Xeo and @Johannes pointed out, I missed the ref-qualifier (aka rvalue for *this) versions:

    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) &> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) const &> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) volatile &> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) const volatile &> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) &&> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) const &&> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) volatile &&> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...) const volatile &&> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...)> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) const> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) volatile> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) const volatile> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) &> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) const &> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) volatile &> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) const volatile &> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) &&> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) const &&> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) volatile &&> : public true_type {};
    template<typename T, typename Class, typename... Args> struct is_member_function_pointer<T (Class::*)(Args...,...) const volatile &&> : public true_type {};
    

    Which is still quite doable. It’s clearer then crazy template magic and numerous helper templates IMHO.

    EDIT2 to clarify, the above are all in an implementation namespace and wrapped around by

    template<typename T> constexpr bool
    is_member_function_pointer() { return __implementation::is_member_function_pointer<remove_cv<T>>::result; }
    

    Where remove_cv is alias template’d to the convenient

    template<typenamen T> using
    remove_cv = typename __implementation::remove_cv<T>::type;
    

    I suppose there are other, and maybe better ways, but this one is at least clear to the reader without any further SFINAE tricks like applied in libc++ or libstdc++, IMHO.

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

Sidebar

Related Questions

I'm trying to implement my own range in D, and I'm having trouble with
I'm trying to implement my own list class but am having trouble reversing just
I'm trying to implement my own List system in Java. the List class file
I'm trying to implement my own GenericIdentity implementation but keep receiving the following error
I am trying to implement my own authentication method for AuthKit and am trying
I'm trying to implement my own version of the 'cd' command that presents the
For all those ligthning fast shop users. I'm trying to implement my own first
Im trying to implement my own Huffman Coding algorithm and the priority queue for
I am trying to implement an own OpenID endpoint based on SMF user accounts.
I am trying to implement my own memory allocation code which is simple yet

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.