Re: Why no conversion from std::initializer_list to array?
Am 02.05.2012 21:12, schrieb Ivan Godard:
I see that there needs to be a constexpr size() for initializer lists,
and actually I'd thought that there was one - perhaps this should be
added independently of my proposal, because I can see other places in
which it might be useful.
Yes, certainly.
However, I don't understand why "ls" has to be constexpr in your example
(or rather, why ars needs a constexpr initializer). Yes, the compiler
needs to save the values on the list at run time rather than than at
compile time, but it does that anyway. And surely the compiler can
subscript the ls list (even though the programmer cannot) to get the
values to use to initialize the ars elements with. I'm ignorant here -
why constexpr?
This is a definition problem: What you are intending (quite reasonably) is a partial constant expression: The size needs to be a constant expression, but not necessarily the contents of the referenced array. How to specify the state of this? I'm not saying that this would be impossible, but it requires some special wording to make that possible. The good news are that this wording needs only be applied to std::initializer_list (which is a "magic" type anyway), but I still consider this as problematic. Note that there are currently no restrictions of std::initializer_list objects compared to other objects. This means, you can legally declare a variable without initializing it with data, like so:
std::initializer_list<int> li; // E.g. at local scope
and at some other place you can assign a new value to it. These are situations, where obviously the li object cannot have an effective constant expression when li.size() is evaluated. Currently we have a uniform way to ensure that a variable can be used as part of constant expressions, this is a declaration of a constexpr variable. Of-course some other expressions can also be used as a constant expression, e.g. a variable with static storage duration can be used to define an address-constant or reference-constant expression but does not require a constexpr specifier. But this holds only for "identity-aware" constant expressions at the moment. There does not exist yet a similar concept for constant "values". But the result of li.size() is such a "value". Either we must invent a syntactic construction (similar to a constexpr specifier) that allows to mark an initializer as a constant value, but not necessarily in all aspects, or we must precisely define the conditions under whi
ch the expression li.size() shall be considered as constant expression, *even though* the complete object li itself may not be declared as a constant expression. I hope that we can realize the latter at some point in time, but it is not a trivial thing and needs careful drafting.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]