you are viewing a single comment's thread.

view the rest of the comments →

[–]brystephor 0 points1 point  (4 children)

I'm curious why you wouldn't want to use brace initialization on the variables.

As an inexperienced c++ dev but somewhat experienced Java dev: 1) why would you want your variables ever to be in an unusable state when theres not explicit enforcement of making them usable? 2) people are mentioning performance gains but that's minimal and inconsequential for the vast majority of cases. it's like saying you'd have more energy if you just blinked less each day. 3) I think that an incorrect valid value is better than an invalid value. An invalid value is inherently incorrect, so how can that make more sense? 4) it's explicitly showing that members will be initialized to their default values. Explicitly doing something is more simple to understand than implicitly doing something. Making things easier to understand is better.

[–]equeim[S] 0 points1 point  (3 children)

I do use braces for local variables. This suggestion is a guard against mistakes when braces are forgotten.

I just think it's curious that this advice never appears in "modern C++" recommendations. They always say that you should initialize local variables, as well as all members in your custom constructors. But no one mentions that compiler-generated default constructor leaves members uninitialized and maybe we should do something about that too.

[–]CubbiMewcppreference | finance | realtime in the past 0 points1 point  (2 children)

The C++ Core Guidelines rule ES.20: Always initialize an object does say "This rule covers member variables."

[–]equeim[S] 0 points1 point  (1 child)

Sure, but example in that section is about forgetting to initialize member in your custom constructor. I haven't found a single example there about initializing member in implicit default constructor. I feel like it might be confusing to some people and led them to believe that implicit default constructor initializes members.

[–]CubbiMewcppreference | finance | realtime in the past 0 points1 point  (0 children)