When you're in a C++ non-static method, you can use the this
variable to refer to the current instance; and through the instance you also have the type. In static methods, I would expect to have something like a this_class
type available to me, which is the type of the class in which I'm implementing the method. Even if it's not inherited (i.e. for class B : public A
, method A::foo()
will have this_class
being A
even when accessed through B
) - it would still be useful, for example when you're implementing a static method in a class with many template arguments, and you want to call some out-of-class function templated on your class's type.
Illustration of use:
template <typename T> void foo() { std::cout << typeid(T).name(); }
template <typename S, typename T, typename U, typename AndLots, typename More>
class A {
/* ... */
static void say_my_name( foo<this_class>(); }
}
So, was this possibility ever considered? Are there reasons it would complicate things for the developer or the compiler?