0

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?

Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
  • 3
    I see a partial solution where you try to get the rest to work. But can you describe what you want to achieve? Maybe your solution with adding attributes with macros sounds very ugly. Maybe there is a c++ way without preprocessor stuff which works. – Klaus Dec 24 '16 at 09:06
  • 1
    how about that? http://stackoverflow.com/questions/21143835/can-i-implement-an-autonomous-self-member-type-in-c – m.s. Dec 24 '16 at 09:31
  • @m.s. - That's a close enough match for what the question is asking that this question could be considered a duplicate. – Omnifarious Dec 24 '16 at 10:26

0 Answers0