A virtual method would require a virtual table, a virtual table would require an instance with a vtable pointer, static member methods are not called through an instance, therefore it is simply not possible.
From the "problem" described in you question, it would appear that you'd expect polymorphic behavior from usage in this format:
Everybody::sayHello();
World::sayHello();
But that doesn't really mandate polymorphism, because you point the kind of functionality you want invoked - it is clear that Everybody::sayHello()
invokes the functionality for Everybody
. There is no "polymorphic ambiguity" - there is no unknown type whose functionality needs to be looked up to produce the expected polymorphic behavior.
Therefore you don't really need dynamic dispatch to solve this problem, you can simply use shadowing - even though you cannot overload static methods as virtual methods, you can still overload by shadowing them, and it is OK, because you specify the type therefore you will get the correct version.
You can either shadow the static methods manually:
struct Base {
static string toWhom() { return ""; }
static void sayHi() { cout << "Hello " + toWhom(); }
};
struct World : Base {
static string toWhom() { return "World"; }
static void sayHi() { cout << "Hello " + toWhom(); }
};
struct Everyone : Base {
static string toWhom() { return "Everyone"; }
static void sayHi() { cout << "Hello " + toWhom(); }
};
Or use a class template to have it done for you, so you only have to shadow the "virtual static method", the template will make sure the correct type to invoke the static method for:
template <typename T>
struct Base {
static string toWhom() { return ""; }
static void sayHi() { cout << "Hello " + T::toWhom(); }
};
struct World : Base<World> {
static string toWhom() { return "World"; }
};
struct Everyone : Base<Everyone> {
static string toWhom() { return "Everyone"; }
};
Then
Everybody::sayHello();
World::sayHello();
both solutions will produce the expected result. There is simply no need for any polymorphism to accomplish this goal. Note that it would certainly be possible to implement exactly what you want, but that would just give you the possibility to create a less efficient solution - because polymorphism has both memory and CPU time overhead, and C++ is a language whose primary concern is performance and efficiency. Therefore it doesn't support a feature that is unneeded, since it is already possible to do what you ask for, and it will be blazing fast, because such simple functions will not even be called - they will be inlined. There is a tremendous performance difference between a function that is inlined and the invoking of a virtual method (like 20x for such trivial functions), and adding another level of indirection for the sake of implementing static virtual members will only make it worse.
I hope now I've given compelling answers to why is this not possible in C++, why it is not needed, and why it makes no sense making it possible in that particular language whatsoever. You basically want to use polymorphism in a scenario that doesn't call for it, for the sake of making the language "easier" - well, you can't have it both ways, C++ is hard because it is fast, just like easier languages are all slower than C++.
Lastly - if you feel like this is such an important language feature to have - you can always request the feature from the standard committee ;)