需要澄清文字类型的定义

时间:2022-11-27 17:09:43

The book I'm reading frequently mentions that a type has to be literal type to use in certain situations, such as types that can use constexpr. But the only definition given was that literal types are arithmetic, reference, or pointer types. But when we define a constexpr type, it seems like it has to be initialized with a literal or a variable that was originally initialized with a literal.

我正在阅读的这本书经常提到,在某些情况下,类型必须是文字类型,例如可以使用constexpr的类型。但唯一给出的定义是文字类型是算术类型,引用类型或指针类型。但是当我们定义一个constexpr类型时,似乎必须使用最初用文字初始化的文字或变量进行初始化。

I'm confused with what qualifies as a literal type and what doesn't.

我对什么是文字类型和什么不符合条件感到困惑。

2 个解决方案

#1


10  

From C++11, 3.9/10:

从C ++ 11,3.9 / 10:

A type is a literal type if it is:

类型是文字类型,如果它是:

  • a scalar type; or
  • 标量类型;要么

  • a reference type; or
  • 参考类型;要么

  • an array of literal type; or
  • 一个文字类型的数组;要么

  • 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),
    • 非静态数据成员(如果有)的brace-or-equal-initializers中的每个构造函数调用和完整表达式都是一个常量表达式(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
    • 它是一个聚合类型(8.5.1)或者至少有一个constexpr构造函数或构造函数模板,它不是复制或移动构造函数,并且

    • all of its non-static data members and base classes are of literal types.
    • 它的所有非静态数据成员和基类都是文字类型。

  • 具有以下所有属性的类类型(第9节):它具有一个简单的析构函数,每个构造函数调用和非静态数据成员(如果有)的大括号中的完整表达式是一个常量表达式(5.19),它是一个聚合类型(8.5.1)或至少有一个constexpr构造函数或构造函数模板,它不是一个复制或移动构造函数,它的所有非静态数据成员和基类都是文字类型。

So basically it's either a reference, or a primitive object type, or something that can be constructed from a literal type in a constexpr-sort of way (arrays, aggregates, or classes with constexpr constructor).

所以基本上它既可以是引用,也可以是原始对象类型,或者可以以constexpr方式(带有constexpr构造函数的数组,聚合或类)从文字类型构造的东西。

#2


3  

C++11 (n3485), [basic.types]9-10

C ++ 11(n3485),[basic.types] 9-10

Arithmetic types, enumeration types, pointer types, pointer to member types, std::nullptr_t, and cv-qualified versions of these types are collectively called scalar types [...]

算术类型,枚举类型,指针类型,指向成员类型的指针,std :: nullptr_t和这些类型的cv限定版本统称为标量类型[...]

A type is a literal type if it is:

类型是文字类型,如果它是:

  • a scalar type; or
  • 标量类型;要么

  • a reference type; or
  • 参考类型;要么

  • an array of literal type; or
  • 一个文字类型的数组;要么

  • a class type 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,
    • 非静态数据成员(如果有)的brace-or-equal-initializers中的每个构造函数调用和完全表达式都是常量表达式,

    • it is an aggregate type or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and
    • 它是聚合类型或至少有一个constexpr构造函数或构造函数模板,它不是复制或移动构造函数,并且

    • all of its non-static data members and base classes are of non-volatile literal types.
    • 它的所有非静态数据成员和基类都是非易失性文字类型。

  • 一个具有以下所有属性的类类型:它有一个简单的析构函数,每个构造函数调用和非正则数据成员(如果有)的大括号中的完整表达式是一个常量表达式,它是聚合类型或至少有一个constexpr构造函数或构造函数模板,它不是复制或移动构造函数,并且它的所有非静态数据成员和基类都是非易失性文字类型。

[dcl.init.aggr]/1

An aggregate is an array or a class with no user-provided constructors, no brace-or-equal-initializers for non-static data members, no private or protected non-static data members, no base classes, and no virtual functions.

聚合是一个数组或类,没有用户提供的构造函数,没有用于非静态数据成员的大括号或等于初始化程序,没有私有或受保护的非静态数据成员,没有基类,也没有虚函数。

#1


10  

From C++11, 3.9/10:

从C ++ 11,3.9 / 10:

A type is a literal type if it is:

类型是文字类型,如果它是:

  • a scalar type; or
  • 标量类型;要么

  • a reference type; or
  • 参考类型;要么

  • an array of literal type; or
  • 一个文字类型的数组;要么

  • 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),
    • 非静态数据成员(如果有)的brace-or-equal-initializers中的每个构造函数调用和完整表达式都是一个常量表达式(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
    • 它是一个聚合类型(8.5.1)或者至少有一个constexpr构造函数或构造函数模板,它不是复制或移动构造函数,并且

    • all of its non-static data members and base classes are of literal types.
    • 它的所有非静态数据成员和基类都是文字类型。

  • 具有以下所有属性的类类型(第9节):它具有一个简单的析构函数,每个构造函数调用和非静态数据成员(如果有)的大括号中的完整表达式是一个常量表达式(5.19),它是一个聚合类型(8.5.1)或至少有一个constexpr构造函数或构造函数模板,它不是一个复制或移动构造函数,它的所有非静态数据成员和基类都是文字类型。

So basically it's either a reference, or a primitive object type, or something that can be constructed from a literal type in a constexpr-sort of way (arrays, aggregates, or classes with constexpr constructor).

所以基本上它既可以是引用,也可以是原始对象类型,或者可以以constexpr方式(带有constexpr构造函数的数组,聚合或类)从文字类型构造的东西。

#2


3  

C++11 (n3485), [basic.types]9-10

C ++ 11(n3485),[basic.types] 9-10

Arithmetic types, enumeration types, pointer types, pointer to member types, std::nullptr_t, and cv-qualified versions of these types are collectively called scalar types [...]

算术类型,枚举类型,指针类型,指向成员类型的指针,std :: nullptr_t和这些类型的cv限定版本统称为标量类型[...]

A type is a literal type if it is:

类型是文字类型,如果它是:

  • a scalar type; or
  • 标量类型;要么

  • a reference type; or
  • 参考类型;要么

  • an array of literal type; or
  • 一个文字类型的数组;要么

  • a class type 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,
    • 非静态数据成员(如果有)的brace-or-equal-initializers中的每个构造函数调用和完全表达式都是常量表达式,

    • it is an aggregate type or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and
    • 它是聚合类型或至少有一个constexpr构造函数或构造函数模板,它不是复制或移动构造函数,并且

    • all of its non-static data members and base classes are of non-volatile literal types.
    • 它的所有非静态数据成员和基类都是非易失性文字类型。

  • 一个具有以下所有属性的类类型:它有一个简单的析构函数,每个构造函数调用和非正则数据成员(如果有)的大括号中的完整表达式是一个常量表达式,它是聚合类型或至少有一个constexpr构造函数或构造函数模板,它不是复制或移动构造函数,并且它的所有非静态数据成员和基类都是非易失性文字类型。

[dcl.init.aggr]/1

An aggregate is an array or a class with no user-provided constructors, no brace-or-equal-initializers for non-static data members, no private or protected non-static data members, no base classes, and no virtual functions.

聚合是一个数组或类,没有用户提供的构造函数,没有用于非静态数据成员的大括号或等于初始化程序,没有私有或受保护的非静态数据成员,没有基类,也没有虚函数。