11

How can I detect a member function has const modifier or not?

Consider the code

struct A {
  int member();
  int member() const;
};

typedef int (A::*PtrToMember)();
typedef int (A::*PtrToConstMember)() const;

I need something like this:

std::is_const<PtrToMember>::value // evaluating to false
std::is_const<PtrToConstMember>::value // evaluating to true 
masoud
  • 55,379
  • 16
  • 141
  • 208
Akon
  • 335
  • 1
  • 11
  • 1
    Should you not know the data types you are dealing with when you are writing the code? – Ed Heal Mar 05 '13 at 05:58
  • 1
    aren't constness of member function checked at compile time? – zzk Mar 05 '13 at 06:01
  • 2
    @EdHeal What if it's a template argument? Then you don't know until the instantiation. Imagine that you want to use it in an `enable_if`. – Agentlien Mar 05 '13 at 06:31

2 Answers2

7

There you go:

#include <type_traits>
#include <iostream>
#include <vector>

template<typename T>
struct is_const_mem_fn {
private:
    template<typename U>
    struct Tester {
        static_assert( // will always fail
            std::is_member_function_pointer<U>::value,
            "Use member function pointers only!");

        // if you want to report false for types other than
        // member function pointers you can just derive from
        // std::false_type instead of asserting
    };

    template<typename R, typename U, typename...Args>
    struct Tester<R (U::*)(Args...)> : std::false_type {};

    template<typename R, typename U, typename...Args>
    struct Tester<R (U::*)(Args...) const> : std::true_type {};

public:
    static const bool value =
        Tester<typename std::remove_cv<T>::type>::value;
};

struct A {
  int member();
  int member() const;
};
typedef int (A::*PtrToMember)();
typedef int (A::*PtrToConstMember)() const;

int main()
{
    std::cout
        << is_const_mem_fn<PtrToMember>::value
        << is_const_mem_fn<const PtrToMember>::value
        << is_const_mem_fn<PtrToConstMember>::value
        << is_const_mem_fn<const volatile PtrToConstMember>::value
        << is_const_mem_fn<decltype(&std::vector<int>::size)>::value;
}

Output: 00111

EDIT: There's a corner case I forgot to account for in the original answer.

The trait above will choke on a hypothetical member function like this:

struct A {
  int member(int, ...) const;
};

because there is no valid specialization of Tester that can be generated for such signature. To fix it, add the following specializations:

template<typename R, typename U, typename...Args>
struct Tester<R (U::*)(Args..., ...)> : std::false_type {};

template<typename R, typename U, typename...Args>
struct Tester<R (U::*)(Args..., ...) const> : std::true_type {};
jrok
  • 54,456
  • 9
  • 109
  • 141
2

Below is a simple type trait adapted from here that should allow this.

template <typename T>
struct is_const_mem_func : std::false_type { };

template <typename Ret, typename Class, typename... Args>
struct is_const_mem_func<Ret (Class::*)(Args...) const> : std::true_type { };
Community
  • 1
  • 1
John Schug
  • 429
  • 1
  • 3
  • 8
  • 2
    Valid for `PtrToMember`, `const PtrToMember` and `PtrToConstMember`. **BUT** wrong for `const PtrToConstMember` – masoud Mar 05 '13 at 08:21