As a question that came up during the discussion of this SO question:
Is it legal, maybe with N3471, to declare a constexpr std::initializer_list
object? Example:
constexpr std::initializer_list<int> my_list{};
Why I think it may not be legal: initializer_list
would have to be a literal type; but are there any guarantees that it is a literal type?
Citations from N3485.
[dcl.constexpr]/9:
A constexpr specifier used in an object declaration declares the object as const. Such an object shall have literal type and shall be initialized.
literal types requirements, [basic.types]/10, sub-bullet class types:
- a class type (Clause 9) that has all of the following properties:
- it has a trivial destructor,
- every constructor call and full-expression in the brace-or-equal-initializers for non-static data members (if any) is a constant expression (5.19),
- it is an aggregate type (8.5.1) or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and
- all of its non-static data members and base classes are of non-volatile literal types.
Bonus points ;) for answering if
constexpr std::initializer_list<int> my_list = {1,2,3,4,5};
is legal (with references). Though I think this is covered by the above + [dcl.init.list]/5
std::initializer_list
functionsconstexpr
(N3741) if we can't legally write the last example. – Herringbone