I have a class:
class my_happy_class
{
// Something here!
};
Now, I am building a macro that one can add inside the class that, depending on some properties of the class, adds a member of a certain type. These properties (e.g., the existence of methods with given signature) can all be easily determined by templates and SFINAE.
class my_happy_class
{
my_happy_macro(my_member); // Adds my_member of a type that is determined from some class properties
};
Now, to determine the class properties, I need to have a way to name it. Obviously, I could extend my_happy_macro
to accept the name of the class as first argument:
class my_happy_class
{
my_happy_macro(my_happy_class, my_member); // Adds my_member of a type that is determined from some class properties
};
But that feels redundant and ugly. So I wonder: is there a way to determine what class I am in autonomously?
class my_happy_class
{
typedef /*(Some super complex expression here that never explicitly says
"my_happy_class", but can use templates, external helper classes,
anything)*/ myself;
// Use myself as an alias for my_happy_class, do the tests, add the member.
};
I tried to use member pointers:
template <typename type> type parent(void (type :: *) ());
class my_happy_class
{
public:
void f();
typedef decltype(parent(&f)) myself;
};
But this doesn't work, because I have to explicitly write &my_happy_class::f
when calling parent
. So now I am short of ideas. Any solution I could use?