简体   繁体   English

枚举 class 声明中的冒号是什么?

[英]What is the colon in an enum class declaration?

I just found this weird piece of code in the align_val_t definition of Visual Studio 2019's standard library:我刚刚在 Visual Studio 2019 标准库的align_val_t定义中发现了这段奇怪的代码:

namespace std
{
    enum class align_val_t : size_t {};
}

What does the colon mean?冒号是什么意思?

enum class align_val_t : size_t {};
//                     ^
//                 this thing

All standard references below refers to N4659: March 2017 post-Kona working draft/C++17 DIS .以下所有标准参考均指N4659:2017 年 3 月 Kona 后工作草案/C++17 DIS


The enum Name: UnderlyingType {} syntax defines an enum that is said to be fixed , with an explicitly specified underlying type . enum Name: UnderlyingType {}语法定义了一个被称为是固定的枚举,具有明确指定的基础类型 From [dcl.enum]/5 :来自[dcl.enum]/5

Each enumeration defines a type that is different from all other types.每个枚举都定义了一个不同于所有其他类型的类型。 Each enumeration also has an underlying type .每个枚举也有一个基础类型 The underlying type can be explicitly specified using anenum-base .可以使用enum-base显式指定基础类型 For a scoped enumeration type, the underlying type is int if it is not explicitly specified.对于作用域枚举类型,如果未显式指定基础类型,则为int In both of these cases, the underlying type is said to be fixed .在这两种情况下,基础类型都被称为是固定的。 [...] [...]


 enum class align_val_t: size_t {}; // ^ // this thing

In this particular example, an enum named align_val_t is defined (in the std namespace) as fixed with explicitly specified underlying type size_t ( std::size_t , to be precise).在这个特定示例中,名为align_val_t的枚举被定义为(在std命名空间中),使用显式指定的底层类型size_t (准确地说是std::size_t固定


Scoped and unscoped enumerations & underlying types作用域和非作用域枚举和基础类型

// Unscoped enumeration; [enum Name {}]
// - underlying type not fixed.
enum UnscopedUnfixed { a, b };
auto uu_a = a;

// Scoped enumeration; [enum class Name {} / enum struct Name {}]
// - underlying type implicitly fixed to int.
enum class ScopedImplicitlyFixed { c, d };
auto sif_c = ScopedImplicitlyFixed::c;

// Scoped enumeration; [enum class Name : TYPE {} / enum struct Name : TYPE {}]
// - underlying type explicitly fixed.
enum class ScopedExplicitlyFixed : unsigned int { e, f };
auto sef_e = ScopedExplicitlyFixed::e;
 enum class align_val_t /*HERE----->*/:/*<------HERE*/ size_t {};

That colon is (an optional) part of the syntax of an enum (class) definition.该冒号是枚举(类)定义语法的(可选)部分。 It separates the name of the enum (class) and the underlying type.它将枚举(类)的名称和基础类型分开。

Since C++11, this is part of the syntax of enum s, for which you can (but don't have to) specify the underlying type.由于 C++11,这是enum语法的一部分,您可以(但不必)指定基础类型。

It's saying that every member of the enum has the type std::size_t ;这是说enum的每个成员都有类型std::size_t exactly that type, nothing else.正是这种类型,仅此而已。 In the olden days, it wasn't always quite so clear what the type would be, at least not by simply looking at the code.在过去,类型是什么并不总是那么清楚,至少不是通过简单地查看代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM