通过成员函数指针值类别进行模板专门化

时间:2018-10-09 12:23:53

标签: c++ templates metaprogramming

我想将成员函数指针的类型传递给类模板,以便该模板可以使用参数包来处理函数参数类型。例如,我想计算成员函数的参数类型大小的总和。为了使它适用于所有值类别,我需要针对不同的函数指针类型提供24个(是否忘记了?)专业化知识:

#include <iostream>

struct X {
    void foo (char) {}
    void cfoo (short) const {}
    void vfoo (short, char) volatile {}
    void cvfoo (int) const volatile {}

    void lfoo (int, char) & {}
    void clfoo (int, short) const & {}
    void rfoo (int, short, char) && {}
    void crfoo (int, int) const && {}

    void vlfoo (int, int, char) volatile & {}
    void cvlfoo (int, int, short) const volatile & {}
    void vrfoo (int, int, short, char) volatile && {}

    void cvrfoo (int, int, int) const volatile && {}

    void vafoo (char, ...) {}
    void vacfoo (short, ...) const {}
    void vavfoo (short, char, ...) volatile {}
    void vacvfoo (int, ...) const volatile {}

    void valfoo (int, char, ...) & {}
    void vaclfoo (int, short, ...) const & {}
    void varfoo (int, short, char, ...) && {}
    void vacrfoo (int, int, ...) const && {}

    void vavlfoo (int, int, char, ...) volatile & {}
    void vacvlfoo (int, int, short, ...) const volatile & {}
    void vavrfoo (int, int, short, char, ...) volatile && {}

    void vacvrfoo (int, int, int, ...) const volatile && {}
};

template <typename TPtr, TPtr Ptr>
struct ArgsSize;

#define DEF_ArgSize_SPEC(qual)      template <typename R, typename C, typename... Args, R (C::*fPtr) (Args...) qual> \
                                    struct ArgsSize<R (C::*) (Args...) qual, fPtr> { \
                                        static constexpr std::size_t value = (0 + ... + sizeof(Args)); \
                                    }; \
                                    template <typename R, typename C, typename... Args, R (C::*fPtr) (Args..., ...) qual> \
                                    struct ArgsSize<R (C::*) (Args..., ...) qual, fPtr> { \
                                        static constexpr std::size_t value = 1000 + (0 + ... + sizeof(Args));  /* For testing, add a big number to denote variadic arguments */  \
                                    };

DEF_ArgSize_SPEC(&)
DEF_ArgSize_SPEC(const &)
DEF_ArgSize_SPEC(&&)
DEF_ArgSize_SPEC(const &&)

DEF_ArgSize_SPEC(volatile &)
DEF_ArgSize_SPEC(const volatile &)
DEF_ArgSize_SPEC(volatile &&)

DEF_ArgSize_SPEC(const volatile &&)


DEF_ArgSize_SPEC()
DEF_ArgSize_SPEC(const)
DEF_ArgSize_SPEC(volatile)
DEF_ArgSize_SPEC(const volatile)

template <auto Ptr>
void test () {
    std::cout << ArgsSize<decltype(Ptr), Ptr>::value << std::endl;
}

int main () {
    test <&X::foo> ();
    test <&X::cfoo> ();
    test <&X::vfoo> ();
    test <&X::cvfoo> ();

    test <&X::lfoo> ();
    test <&X::clfoo> ();
    test <&X::rfoo> ();
    test <&X::crfoo> ();

    test <&X::vlfoo> ();
    test <&X::cvlfoo> ();
    test <&X::vrfoo> ();

    test <&X::cvrfoo> ();

    test <&X::vafoo> ();
    test <&X::vacfoo> ();
    test <&X::vavfoo> ();
    test <&X::vacvfoo> ();

    test <&X::valfoo> ();
    test <&X::vaclfoo> ();
    test <&X::varfoo> ();
    test <&X::vacrfoo> ();

    test <&X::vavlfoo> ();
    test <&X::vacvlfoo> ();
    test <&X::vavrfoo> ();

    test <&X::vacvrfoo> ();
}

我使用了一个宏,因为即使对于这个简单的示例,也有很多重复项。在“ ArgsSize”内部,我使用结果类型(R),类(C)和参数类型(Args ...)。有更好/更短的方法吗?

0 个答案:

没有答案