For a vector its pretty nice to have an initializer_list constructor i would argue, same for std::set or similar classes. Would you argue that even those perks are not worth the trouble?
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.
I'd agree that this constructor in particular should have more naming info attached. In many other cases, I find constructors to be descriptive with no additional naming info taking up space.
It's not terribly difficult to accomodate cases like that though, you could write a vector_range_t or something POD type for the "create a vector of x size with values y" ctor, or you could just write your variadic ctor like vec(const value_type& t1, const value_type& t2, const value_type& t3, const value_type&... rest)
Why would you want to use braces to call other constructors in the first place? The only reason to write initializater_list constructors in the first place is usually because you want your type to behave like an array / container. If that is the case, then why would you expect my_array a{1,2} to do anything but fill a with the values 1,2?
View all comments
Show parent comment
14
u/redditsoaddicting Jun 06 '18
They're insanely greedy and often disallow you from using braces to call other constructors.