различать указатель на перегруженную функцию-член в C ++ - PullRequest
0 голосов
/ 25 апреля 2011

Я хочу различать перегруженную функцию-член в структуре шаблона C ++.Статический метод get_pointer из специализированной структуры distinguish_foo должен возвращать указатель на derived::foo , если этот метод существует .В противном случае я хочу вернуть указатель на pseudo::foo.
Как мне этого добиться?

struct sample
{
    void foo()              { std::cout << "void sample::foo()" << std::endl; }
    void foo(int)           { std::cout << "void sample::foo(int)" << std::endl; }
    void foo(int, int)      { std::cout << "void sample::foo(int, int)" << std::endl; }
    void foo(int, int, int) { std::cout << "void sample::foo(int, int, int)" << std::endl; }
};

struct other
{
    void foo()              { std::cout << "void other::foo()" << std::endl; }
    void foo(int)           { std::cout << "void other::foo(int)" << std::endl; }
    void foo(int, int)      { std::cout << "void other::foo(int, int)" << std::endl; }
    void foo(int, int, int) { std::cout << "void other::foo(int, int, int)" << std::endl; }
};

struct derived : sample, other
{
    using sample::foo;
    using other::foo;
};


template<typename signature>
struct distinguish_foo;

template<class C, typename R>
struct distinguish_foo<R (C::*)()>
{
    typedef char(&unique)[0xffff];
    struct pseudo { unique foo(); };
    struct host : C, pseudo {};

    template<typename R>
    static R (host::*get_pointer())() { return &host::foo; }
};

template<class C, typename R, typename P0>
struct distinguish_foo<R (C::*)(P0)>
{
    typedef char(&unique)[0xffff];
    struct pseudo { unique foo(P0); };
    struct host : C, pseudo {};

    template<typename R>
    static R (host::*get_pointer())(P0) { return &host::foo; }
};

template<class C, typename R, typename P0, typename P1>
struct distinguish_foo<R (C::*)(P0, P1)>
{
    typedef char(&unique)[0xffff];
    struct pseudo { unique foo(P0, P1); };
    struct host : C, pseudo {};

    template<typename R>
    static R (host::*get_pointer())(P0, P1) { return &host::foo; }
};

template<class C, typename R, typename P0, typename P1, typename P2>
struct distinguish_foo<R (C::*)(P0, P1, P2)>
{
    typedef char(&unique)[0xffff];
    struct pseudo { unique foo(P0, P1, P2); };
    struct host : C, pseudo {};

    template<typename R>
    static R (host::*get_pointer())(P0, P1, P2) { return &host::foo; }
};
...