0

I want to distinguish between an overloaded member-function in a C++ template struct. The static method get_pointer from specialized struct distinguish_foo should return a pointer to derived::foo if this method exists. Otherwise I want to return a pointer to pseudo::foo.
How can I achieve this?

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; }
};
skypjack
  • 49,335
  • 19
  • 95
  • 187
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
  • 2
    Why do you think you want to do this? –  Apr 25 '11 at 19:33
  • It's my attempt to describe my problem in more detail to achieve this: http://stackoverflow.com/questions/5747205/check-for-member-existence-in-c – 0xbadf00d Apr 25 '11 at 19:50
  • What would you do with the result? It would not be possible to call it with a `derived` object. – n. m. could be an AI Jan 01 '17 at 15:33
  • @n.m. and possibly others. Please note that the original question is over 5 years old and was dug up by an edit. It looks quite case-specific, it might be quite outdated. OP seems to be still active though. – luk32 Jan 01 '17 at 15:36

0 Answers0