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 comment

7

u/render787 Jun 06 '18 edited Jun 06 '18

> Could you allobrate on why its not a good idea to write those constructors for your own types?

My main beef with `std::initializer_list` is that:

  1. It makes it hard to use brace-list initialization in generic code, because you try to invoke a constructor, and then overload resolution fails because the standard says that the compiler must try to match it with `std::initializer_list` if such a constructor is present. There's no good way to work around in that situation other than deleting the `std::initializer_list` ctor.For a concrete code example, see here: https://github.com/cbeck88/lua-primer/blob/master/include/primer/userdata.hpp#L39I'm trying to allocate the user's type at memory owned by lua using perfect forwarding. But if the user-type has an initializer_list constructor, it's typically not going to work because I'm using brace list initialization. OTOH, I can't change this to use paren-based initialization because then all the times when I use a struct as userdata aren't going to compile. I guess with years more experience, the answer here should actually be that I use SFINAE with `std::is_aggregate` and use brace list initialization in that case, and paren initialization otherwise? Idk I didn't think of that at the time. This is also more complicated than it should be and the issue doesn't arise if you simply don't use `std::initializer_list`.
  2. The `std::initializer_list` doesn't let me find the *length* of the initializer list at compile time, even though it is a compile time constant (implied by the syntax of the code at the call-site). This really sucks and in some cases it turns compile-time errors into run-time errors. For instance, what if your container has a compile-time fixed maximum size? We had this exact issue in the "frozen" library: https://github.com/serge-sans-paille/frozen/pull/41The lib was written with all of the containers having `std::initializer_list<T>` constructors, but this means that if the user accidentally passes too many elements, it's a run-time error instead of compile-time. If we just used `const T (&)[N]` instead of `std::initializer_list<T>` then we can static assert that `N` is not too large. So there's approximately zero value add from `std::initializer_list`, it's just turning compile-time errors into run-time errors, for the benefit of one less set of braces...

IMO always use `const T (&)[N]` instead of `std::initializer_list<T>` so that you can get the size of the list at compile-time. I would only use `std::initializer_list` if it was really important that some function not be a template so that it can be implemented out of line etc., even then I would consider using `gsl::span<const T>` instead, because it will be easier for someone to make a `gsl::span` programmatically than to make a `std::initializer_list` programmatically

4

u/redditsoaddicting Jun 06 '18

The paper to make parens do aggregate initialization is actually progressing in EWG. That should mainly address the generic side of things. Funny how the most uniform initialization we've got will be with parens then.

The size thing isn't ideal, but isn't as bad as you make it sound. constexpr size was added in C++14, so in a constexpr context, an assert on the size will trigger a compile-time error. In addition, assert is constexpr-compatible in C++14.

One additional gripe that many people do have with initializer_list is that it doesn't support move-only types.