BigBahss/vscode-cmantic

Sort the initializer list to the order the members appear in the header

Wastus opened this issue · 3 comments

The members are initialized in the order they appear in the header (if I'm not mistaken). So ordering the initializer list to reflect the header would be nice to tidy things up.

Of course this would need a detection, that the list is not in order in the first place.

I kind of like this. Are you suggesting that this be sort of like a 'diagnostic' that would let you know when the list is not in order and prompt you to auto-fix it? If not, this could probably be done with a simple command, given that the user already knows it's out of order. Also, I've heard conflicting information on whether or not the order of initializers actually matter, so I would want to confirm that first.

I just looked it up, the standard says the order doesn't matter, here's the snippet from cppreference:

Initialization order

The order of member initializers in the list is irrelevant: the actual order of initialization is as follows:

  1. If the constructor is for the most-derived class, virtual bases are initialized in the order in which they appear in depth-first left-to-right traversal of the base class declarations (left-to-right refers to the appearance in base-specifier lists)
  2. Then, direct bases are initialized in left-to-right order as they appear in this class's base-specifier list
  3. Then, non-static data member are initialized in order of declaration in the class definition.
  4. Finally, the body of the constructor is executed

(Note: if initialization order was controlled by the appearance in the member initializer lists of different constructors, then the destructor wouldn't be able to ensure that the order of destruction is the reverse of the order of construction)

Found here: https://en.cppreference.com/w/cpp/language/constructor

Since the order doesn't matter, this becomes more of a 'style' issue, which I'll leave up to the code formatters. Thank you for the submission though. I'm going to close this.

Okay, that's fair enough. My idea was indeed that it would be a diagnostic (at best). But yes, it's for style.