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.
C++ does not have any feature to get the name of the current class, namespace, etc. In C++11 you can get type of the variable, but you need the variable in the first place. In this case you do not have anything to start with.