Pointer to current function

前端 未结 4 1279
难免孤独
难免孤独 2020-12-19 13:06

Is there any way to get a pointer to the current function, maybe through gcc extensions or some other trickery?

Edit I\'m curious whether it is poss

4条回答
  •  猫巷女王i
    2020-12-19 13:11

    This isn't especially portable, but should work on at least some platforms (i.e., Linux and OSX, where I can check the documentation; it definitely doesn't work on Windows which lacks the API):

    #include 
    
    // ...
    void *handle = dlopen(NULL, RTLD_LAZY);
    void *thisfunction = handle ? dlsym(handle, __FUNCTION__) : NULL;
    if (handle) dlclose(handle); // remember to close!
    

    There are a number of other less-portable shortcuts that work on some platforms but not others. This is also not fast; cache it (e.g., in a local static variable) if you need speed.

提交回复
热议问题