寻找成员函数的恒定性

时间:2013-03-05 05:51:45

标签: c++ const typetraits member-functions

如何检测成员函数是否具有const修饰符?

考虑代码

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

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

我需要这样的东西:

std::is_const<PtrToMember>::value // evaluating to false
std::is_const<PtrToConstMember>::value // evaluating to true 

2 个答案:

答案 0 :(得分:7)

你去了:

#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;
}

输出:00111

编辑:我在最初的答案中忘记了一个角落案例。

上面的特征会阻碍这样一个假想的成员函数:

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

因为没有可以为此类签名生成的Tester的有效专门化。要修复它,请添加以下特化:

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 {};

答案 1 :(得分:2)

下面是一个简单的类型特征改编自here,应该允许这样做。

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 { };