r/cpp • • Jun 05 '18

Initialization in Modern C++

https://twitter.com/timur_audio/status/1004017362381795329
379 Upvotes

51 comments sorted by

View all comments

Show parent comments

3

u/redditsoaddicting Jun 06 '18

I love being able to easily initialize containers with a list of elements, as well as other clever uses like a JSON object. However, I don't think making it the same syntax used to call other constructors was a good choice. It's hard to say whether a plain list constructor is worth the trouble to me or if I'd consider an alternate best-we've-got constructor like vector foo = list_of(1, 4, 5); or vector foo{from_list, 1, 4, 5};, where either would be uniform across classes that use it. If I could have my way, I'd probably prefer an alternate syntax. For example, vector foo{[1, 4, 5]};.

6

u/[deleted] Jun 06 '18

What about this

std::vector<int> foo{1, 2}; // braced initialisation
std::vector<int> foo{{1, 2}}; // initialiser list

1

u/redditsoaddicting Jun 06 '18

As far as I remember, this can run into its own corner cases, unfortunately.

1

u/[deleted] Jun 06 '18

I'm not surprised. "My" syntax seems rather obbvious and I'm definitely not the first one who thought of it. I was just hoping someone would say what's the corner case here.

→ More replies (0)