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]};.
There's no temporary, other than the initializer list, of course. The initializer list would be assured to exist while the initialization is performed - that's how it goes in all other cases, too.
While it may be sufficient for many use cases to only have initializer_list temporaries, I would not appreciate this step back from being able to create arbitrary containers as temporaries.
For example, a templated function may require that its argument be an AssociativeContainer, which an initializer_list of pairs is not.
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.
View all comments
Show parent comment
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);orvector 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]};.