0

To clarify, I'm trying to create a macro which makes taking member function addresses a little easier, so I would hope to input a member function name of the current class (and only the member function name), and the macro will use the type of the variable or the type of the enclosing class to actually access the function. e.g.

#define GETMEMBERFUNCTIONPOINTER(identifier) /*magic code goes here*/

struct myStruct {
    void (myStruct::*functionPointer)();
    void myMemberFunc() {

    }
    myStruct() {
        functionPointer = GETMEMBERFUNCTIONPOINTER(myMemberFunc);
    }
};

is this possible? The closest I've managed is to have each class inherit a private typedef which can be used in the macro

TechNeko
  • 35
  • 5
  • Wow, for all the "Frankenstein monster" developments in C++, it still doesn't allow just `&myMemberFunc`. Interestingly, GNU C++ allows it as an extension if you invoke it with `-fpermissive`. – Kaz Apr 02 '19 at 02:47
  • @Kaz "Frankenstein monster" is a good way to describe how I code, so a great deal of me using c++ is just "how come I can't get a pointer to the destructor! How come I can't get a straight pointer to the member function! How come I can't write this into a typedef!". It might be a bad sign. Good to know about GNU C++ though. – TechNeko Apr 02 '19 at 02:55

1 Answers1

1

Disclaimer: Macros are evil. Do not use macros.

That being said, I believe the magic code you're looking for would be

#define GETMEMBERFUNCTIONPOINTER(identifier) (&std::remove_pointer_t<decltype(this)>::identifier)

working example here

I do not believe that anyone should be actually doing this, however. If your design requires member function pointers to be taken so frequently that you find yourself wanting for a macro to do it for you, then I highly recommend to seriously question whatever it is that you're doing before you proceed…

Michael Kenzel
  • 15,508
  • 2
  • 30
  • 39
  • Thanks so much! It works perfectly! Believe me I hear the "I think you need to rethink what you're doing" line a great deal, and I get it, but I can't help but want to try really far out options that usually go against code standards. I usually figure out why it's against code standards, or find that it's weird but functional. Thanks again! – TechNeko Apr 02 '19 at 03:06
  • @TechNeko Well, you have been warned. break a leg ;-) – Michael Kenzel Apr 02 '19 at 03:13