声明一个constexpr initializer_list对象是否合法? [英] Is it legal to declare a constexpr initializer_list object?

查看:179
本文介绍了声明一个constexpr initializer_list对象是否合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为在讨论此SO问题时出现的问题:

As a question that came up during the discussion of this SO question:

是合法的,也许是 N3471 ,声明一个 constexpr std :: initializer_list 对象?示例:

Is it legal, maybe with N3471, to declare a constexpr std::initializer_list object? Example:

constexpr std::initializer_list<int> my_list{};






为什么我认为这可能不合法: code> initializer_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?

来自N3485的引文。

Citations from N3485.

[dcl.constexpr ] / 9:

[dcl.constexpr]/9:


在对象声明中使用的constexpr说明符将对象声明为const。这样的对象应该有
字面类型,并且应该初始化。

A constexpr specifier used in an object declaration declares the object as const. Such an object shall have literal type and shall be initialized.

字面量类型需求,[basic.types] / 10 ,子项目类类型:

literal types requirements, [basic.types]/10, sub-bullet class types:



  • 一个类类型(第9条),具有所有以下属性:

    • 它有一个简单的析构函数,

    • 每个构造函数调用和全表达式在大括号或-static数据成员(如果有)是常量表达式(5.19),

    • 它是一个聚合类型(8.5.1)或至少有一个constexpr构造函数或构造函数模板复制或移动构造函数,并且

    • 其所有非静态数据成员和基类都是非易失性字面值类型。

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

奖励积分; p>


Bonus points ;) for answering if

constexpr std::initializer_list<int> my_list = {1,2,3,4,5};

是合法的(带引用)。虽然我认为这是由上述+ [dcl.init.list] / 5

is legal (with references). Though I think this is covered by the above + [dcl.init.list]/5

推荐答案

更新: / strong>解决 CWG问题后,情况变得更复杂DR 1684 删除了下面引用的要求。有关更多信息,请参见此讨论在std讨论邮件列表和相关问题为什么不将`std :: initializer_list`定义为文字类型?

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:

[decl.constexpr]/8:


非构造函数的非静态成员函数的constexpr说明符声明成员函数为const(9.3.1)。 [...]这个函数的类是一个成员的类应该是一个文字类型(3.9)。

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

N3471保证 std :: initializer_list 将是文字类型。

Therefore, the changes of N3471 guarantee std::initializer_list will be a literal type.

注意 constexpr ctor单独不需要 std :: initializer_list 是一个文字类型, .constexpr] / 4 + 8。旁注: constexpr ctor的非文字类型的对象可以在常量初始化期间初始化 [basic.start.init] / 2] 的一部分)。

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

这篇关于声明一个constexpr initializer_list对象是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆