Christian Hanné <
the....@gmail.com> wrote:
> I said why this confuses the reader. With constructors you have
> the declaration of the construtor in a tooltip in your IDE, with
> designated initializers you have to check the structure definition
> first.
Firstly, if understanding the code requires an IDE, then the code is badly
designed.
Secondly, if an IDE is able to show the constructor signature in a tooltip,
then certainly it can show the members of a struct in a designated
initalizer list? If not, and you rely so much on IDE tooltips, then get
a better IDE.
Thirdly, designated initializers make initialization a lot clearer
because they function, essentially, as named parameters, which is
something that constructors (and functions in general) have always lacked
in C++.
This is particularly handy for configuration headers and such, which may
be written and fine-tuned by team members other than the original programmer
(some of which may not be programmers at all). I have used this quite
extensively, to create "configuration" files like:
const Settings kSettings
{
.numberOfStuff = 123,
.aValueForSomething = 5,
.aFactorUsedForThis = 0.25,
.elements =
{
{ .amount = 5, .factor = 2.2, .seconds = 10 },
{ .amount = 8, .factor = 1.0, .seconds = 15 },
{ .amount = 1, .factor = 0.5, .seconds = 5 }
}
};
Even a complete non-coder can go and fine-tune those values without the
need to understand C++ (especially when the variables are named clearly
according to the context; obviously I used fictitious names above).
(In fact, I have used that technique quite a lot in some Objective-C++
programs, where the order of initialization doesn't need to match the
order of declaration. This is particularly handy because it allows the
initialization to use an ordering that's easy to read and understand,
while the declaration may use an ordering that's eg. optimized for
space, as the order of declaration of member variables matter in this
regard. It's a huge same that C++20 decided to put the limitation that
the initializers must be in the same order as the declaration.)