Dispelling Common C++ Myths: Static Constructors

Someone may be asked at a C++ interview: What is a static constructor?.
Possible, one interviewer would expect answers like “A static constructor is one called only once” and/or “A static constructor is called before any other instance”, and so on.
Probably, such guy is a little bit confused by another language background (e.g. C# or C++/CLI), or is one learning some concepts and patterns by heart, or is influenced by some misleading articles found over the Internet.

The right answer is neat and clear: “There is no static constructor in standard C++ programming language“.
Not mentioning other arguments, let’s just quote from the C++ standard:

ISO/IEC 14882:2011
12.1 Constructors
4. A constructor shall not be virtual (10.3) or static (9.4).

Still don’t believe that? No problem, even just for fun, try compile next code using any C++ compiler:

Concluding, just have a look then may forget these links if you are a C++ programmer…

…but don’t forget that neither C#, nor C++/CLI is (standard) C++!

16 thoughts on “Dispelling Common C++ Myths: Static Constructors

  1. Tudor

    Eh, daca erau static constructors in C++, ai fi avut ocazia sa scrii o serie de n-șpe articole pe tema asta :) , precum cele de la http://ericlippert.com/2013/02/06/static-constructors-part-one/ despre static constructors in C#
    Oricum, articolul acela incepe cu un sfat bine intentionat:
    “Static constructors and destructors are the two really weird kinds of methods, and you should do as little as possible in them”

    Reply
    1. Ovidiu Cucu Post author

      That’s it, “C# is easier than C++”. Or isn’t?…
      Anyway, I’m feeling better now: C++ is not the only one modern programming language, with stuff that “you should do as little as possible…” :D

      Reply
    2. Sasha

      Pai si de ce sa eviti sa folosesti static constructors, doar pentru ca asa a zis Eric Lippert? O alta recomandare ar fi sa nu folosim focul pentru ca ne putem arde.

      Reply
      1. Tudor

        Da, pentru un developer care crede ca un static contructor in C# e o chestie.. simpla, raspunsul poate incepe cu “asa a zis Eric Lippert” dupa care poate citi articolele respective pentru a intelege de ce.. :) (pentru C++ people here: Eric Lippert in “lumea C#” e echivalentul unui Sutter sau Koenig ..)

        Reply
  2. Sasha

    Good point, but the correct answer should be another question: “To what standard/compiler are you referring?”, if you the interviewer doesn’t know is a good chance to prove that you know more than him – either he will appreciate this, either he will not and this case: don’t worry be happy, because this was not the company for you! If the interviewer already knew this he will appreciate you.

    Anyway, also in C# there is no real difference at jitter time (the real compilation, which uses afterall a C++ compiler). The difference is an extra-flag in the IL code – stating when the constructor will be called by the CLR. No static keyword means beforefieldinit flag set, otherwise the ctor will be called in a deterministic manner exactly before the first access to any method or field of the class.

    Off-topic: If the interviewer is only learning some stuff by heart for me looks like a former Communist Party activist, or modern day Microsoft .net evangelist.

    Reply
    1. Ovidiu Cucu Post author

      …or you can answer with the question “How much time can you give me to discuss this subject?”
      Of course, this case you can also have a chance to be politely invited: “Please, close the (company) door… from outside!” :)

      Reply
  3. frank

    No static constructors in C++?
    I’d rather say a constructor in C++ is always static but you cannot explicitly state it.
    After all, you do call a constructor on a class, not on an object (although that is not impossible).

    Reply
    1. Sasha

      @frank Can you give some arguments when you say “I’d rather say a constructor in C++ is always static but you cannot explicitly state it.”?

      Reply
      1. frank

        My argument was, slightly differently stated, that you call a constructor before you have an object. Hence, you call it on the class.
        But, as Marius Bancila correctly points out, you do not call the scope resolution operator.

        Reply
        1. Ovidiu Cucu Post author

          Partially you’re right. Given your criteria, someone may look at a constructor as at a static member function.
          However, let’s quote again the “C++ Holy Book”:

          ISO/IEC 14882:2011
          9.4.1 Static member functions
          2. [ Note: A static member function does not have a this pointer (9.3.2). —end note ]

          A constructor HAS a this pointer, so it’s clearly NOT static.

          Reply
          1. Marius Bancila

            The point is, memory is allocated first for the object, and after than the constructor is called not to construct=allocate, but to initialize the object. And the constructor is called on the object, except that the syntax is a little bit different than with the other calls. After all their some special functions, right?

          2. frank

            I did not look at it that way, but you are absolutely right. And, given the this pointer criterium, it really is undisputable. Thanks for the enlightment :-)

        2. Ovidiu Cucu Post author

          There’s no much “enlighting” in this article, other than stating a simple statement: “There is no static constructor in standard C++ programming language.”
          However, it grabbed a bunch of negative feedbacks.
          That’s good, I know C++ programmers as ones splitting hairs in four pieces.
          That’s great! Not all C++ programmers are dead… :D

          Reply
  4. Erik

    I saw a post recently where someone referred to a factory member function as a “static constructor”. He was speaking metaphorically, but maybe that’s another answer the interviewer would accept; a static class member function that creates a new instance of the class and returns a pointer to it. If you make all of the constructors private then that’s the only way to get a new instance of the class so, yeah, you have a static constructor (in a manner of speaking).

    Reply
    1. Tudor

      No quite – what other languages understand by “static constructor” is not a way to create new object instances, but just a way to initialize class-level fields/properties that are not associated to any object instance..

      Reply

Leave a Reply