A blog about C++, music software, tech community, and life.

Initialisation in C++17 – the matrix

Recently, I gave a talk titled “Initialisation in modern C++”, which was apparently quite well received. (A video recording is available here).

One of the slides in this talk was a matrix tabulating all the possible initialisation syntaxes and what they do for different kinds of types (such as: built-in types, aggregates, etc.)

Ever since then, I keep getting requests for a high-resolution version of that table. So finally I got around to uploading one. Here it is:

You can freely reuse this table – I hope it will be useful. And please let me know if you spot any mistakes 🙂

Keep in mind that the above table is for C++17, but things will change in C++20, in particular for aggregate types: we will get designated initialisers as well as direct initialisation for aggregates.

3 Comments

  1. Isaac Monteiro

    Such a nice quick Referenz table!
    I’m Just starting to learn C++ but this seems to be a very important topic.
    Also, under types w/o user provided ctor | value-init/empty-braces, wouldn’t it be
    Value Initalization(i.e. call default ctor for class types, Zero Initialize otherwise)?

    • Timur Doumler

      Technically, yes, but the effects of value initialisation will actually be zero initialisation in this case, if I interpret this correctly: http://eel.is/c++draft/dcl.init#8.2

    • Paul Omta

      Since you’re learning C++, may I suggest to stick to the curly braces as much as possible? They are more consistent between the different cases than the old syntax with braces.

Leave a Reply to Isaac Monteiro Cancel reply

Your email address will not be published. Required fields are marked *

© 2020 timur.audio

Theme by Anders NorénUp ↑