gpt4 book ai didi

c++ - 需要澄清文字类型的定义

转载 作者:IT老高 更新时间:2023-10-28 21:52:54 29 4
gpt4 key购买 nike

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

我很困惑什么是文字类型,什么不是。

最佳答案

从 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),
    • 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 构造函数的类)。

关于c++ - 需要澄清文字类型的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18581443/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com