Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Wastus opened this issue Feb 25, 2021 · 3 comments
Closed
Labels
enhancement New feature or request

Comments

@Wastus
Copy link

Wastus commented Feb 25, 2021

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.

@BigBahss
Copy link
Owner

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.

@BigBahss BigBahss added the enhancement New feature or request label Feb 25, 2021
@BigBahss
Copy link
Owner

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.

@Wastus
Copy link
Author

Wastus commented Feb 25, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants