dimanche 26 mai 2019

Get the name of an overloaded function passed as an argument at run-time

Assume all includes are defined, please go through the code structure defined below.

std::unordered_map<std::string, void*> callbackReg;

class A
{
public:
    void foo(int a)
    {
        //impl
    }
    void foo(int a, int b)
    {
        //impl
    }
    void foo(int a, double d)
    {
        //impl
    }

};


template<class Instance , typename Function>
void register_func(Instance& obj, Function func)
{
    /* have to store function name as the key and 
     - pointer to raw member function pointer as the value, 
     - in callbackReg Map */
    // ---> Point B
}

int main(int argc, const char * argv[])
{
    A a;
    register_evt(a, &A::foo); //---> point A
    system("Pause");
}


Problems and Discussions

  1. @ Point A how to resolve function name scope resolution issue which means how to call a overloaded function/pointer to such function ? without inline static cast since it makes bit ugly and messy.
  2. @ Point B assume we have fixed overload name resolutuin issue and now we have to store member funtion name and its pointer value in callbackReg map.

I know may this could bad cumbersome approach but, is this feasible in C++? or else is there any way to store raw memeber funtion pointers with some other key-value in order to use them as callbacks.Since at debug time,debugger already knows what is the function name passed as register_func template fucntion argument. Please look at the image attached. enter image description here

  • Therefore,simply, how can we implement callback register/unregister scenario using only raw member/non-member function pointers?

(Please if the solution or methodology is a cross-platform approach that would be grateful, at least Win32 based)

Aucun commentaire:

Enregistrer un commentaire