Is it legal to declare a constexpr initializer_list object?

前端 未结 1 942
春和景丽
春和景丽 2020-11-29 08:34

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

相关标签:
1条回答
  • 2020-11-29 08:41

    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).

    0 讨论(0)
提交回复
热议问题