3

I want to pass the type of a member function pointer to a class template, so that the template can use a parameter pack for working with the function parameter types. As an example, I would like to calculate the sum of the sizes of the parameter types for a member function. For this to work with all value categories, I'd need to provide 24 (did I forget any?) specializations for different function pointer types:

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

I used a macro because even for this trivial example, there's a lot of duplication. Inside "ArgsSize", I work with the result type (R), class (C) and parameter types (Args...). Is there a nicer/shorter way to do this?

Erlkoenig
  • 2,664
  • 1
  • 9
  • 18
  • 2
    You miss variants as `Ret (C::*) (Args...)` (without `&`, `&&`) and with C-ellipsis (`Ret (C::*) (Args... ...)` as `printf`). – Jarod42 Oct 09 '18 at 12:41
  • Oh right! I thought nothing and "&" would mean the same thing. Now it looks even worse :) – Erlkoenig Oct 09 '18 at 12:56
  • I would say create (or use some library) for function traits to have the boilerplate once, then use something like `(method_traits::Args + ...) + 1000 * method_traits::has_ellipsis + 42 * method_traits::is_const`. – Jarod42 Oct 09 '18 at 13:59

0 Answers0