C++ type of enclosing class in static member function

The problem is that C++ is lacking a self keyword.

I typically write:

struct Foo
{
   typedef Foo self;

   static void bar()
   {
      self* ptr = nullptr;
   }
};

I realise you still have to make sure the typedef is correct, but at least this way you can have it at the top of the type definition where you’ll notice it.

With hackery, though, you can make this entirely autonomous.

Leave a Comment