What is the rationale for not having static constructor in C++?
If it were allowed, we would be initializing all the static members in it, at one place in a very organized way, as:
//illegal C++
class sample
{
public:
static int some_integer;
static std::vector<std::string> strings;
//illegal constructor!
static sample()
{
some_integer = 100;
strings.push_back("stack");
strings.push_back("overflow");
}
};
In the absense of static constructor, it's very difficult to have static vector, and populate it with values, as shown above. static constructor elegantly solves this problem. We could initialize static members in a very organized way.
So why doesn't' C++ have static constructor? After all, other languages (for example, C#) has static constructor!
main()
. – Lembergmain()
: see this : #4783904 – Lembergmain
), in "implementation defined" order. I see nothing impossible in having static constructors in C++, my opinion is that they just forgot/didn't want to add extra complexity to compilers/supposed thatstatic
objects were enough for these purposes. – Rencontrestatic
objects initialization (see §3.6.2). – Rencontrestatic
constructors execution order has been already solved for constructors ofstatic
objects as I expected (actually it's not "implementation defined" but "unspecified"). – Rencontre