Hacker News new | ask | show | jobs
by amluto 715 days ago
I’m surprised there were no snarky comments about:

> So, here’s the glue between list-initialization and aggregate initialization: if list-initialization is performed on an aggregate, aggregate initialization is performed unless the list has only one argument, of type T or of type derived from T, in which case it performs direct-initialization (or copy-initialization).

The word “unless” is even bold.

We have fancy syntax:

    T t{v0};
And we also have:

    T t{v0, v1};
And so on. But the one-element case does not reliably work like the 2+-element case. And this is in a language that increasingly works toward making it straightforward to create a struct from a parameter pack and has support for variable length array-ish things that one can initialize like this. And the types can, of course, be templated.

So you can write your own constructors, and you can initialize a tuple or array with only one element supplied, and you might trip over the wrong constructor being invoked in special cases.

I remember discovering this when C++11 initializer lists were brand new and thinking it was nuts.

1 comments

Initializer lists is irreverent, nobody uses it anyway. Other than a few standard containers that use it, you can completely ignore the silly thing.
As is the nature of bad design, “nobody uses it other than some people sometimes” is a silly sentiment and indicative of a problem.
That's the biggest problem with them. Something isn't as dangerous when it's in your face and you need to confront it on a regular basis. What's dangerous are the things that rarely need to think about, except for that very rare moment when it bites you in the ass precisely because it's not on your mind.
I've definitely seen initializer lists recommended as best-practice in safety-critical code.