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
Update: The situation got a bit more complicated after the resolution of CWG DR 1684 removed the requirement quoted below. Some more information can be found in this discussion on the std-discussion mailing list and in the related question Why isn't `std::initializer_list` defined as a literal type?
[decl.constexpr]/8:
A constexpr specifier for a non-static member function that is not a constructor declares that member function to be const (9.3.1). [...] The class of which that function is a member shall be a literal type (3.9).
Therefore, the changes of N3471 guarantee std::initializer_list
will be a literal type.
Note the constexpr
ctor alone doesn't require std::initializer_list
to be a literal type, see [dcl.constexpr]/4+8. Side note: An object of non-literal type with constexpr
ctor can be initialized during constant initialization [basic.start.init]/2] (part of static initialization, performed before any dynamic initialization).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With