需要澄清文字类型的定义 [英] Need clarification on definition of literal type

查看:131
本文介绍了需要澄清文字类型的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我很困惑

推荐答案

从C ++ 11,3.9 / 10:

From C++11, 3.9/10:


如果是类型,则为类型:

A type is a literal type if it is:


  • 一个标量类型;或

  • 引用类型;或

  • 一个文字类型数组;或

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

    • 它有一个小的析构函数,

    • 非静态数据成员(如果有)的每个构造函数调用和全表达式在大括号或等于初始化器中是一个常量表达式(5.19),

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

    • 其所有非静态数据成员和基类都是文字类型。

    • 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),
      • 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 literal types.

      所以基本上是一个引用,或一个基本的对象类型,或者可以从一个字面量类型构造成一个constexpr - 排序方式(数组,聚合或类与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).

      这篇关于需要澄清文字类型的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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