Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Language Reference

table of contents

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

Enums

Contents
  1. Named Enums
    1. Enum Member Values
    2. Enum Properties
    3. Enum Copying and Assignment
  2. Anonymous Enums
    1. Single Member Syntax
  3. Manifest Constants
EnumDeclaration:enumIdentifierEnumBodyenumIdentifier:EnumBaseTypeEnumBodyAnonymousEnumDeclaration
EnumBaseType:Type
EnumBody:{EnumMembers};
EnumMembers:EnumMemberEnumMember,EnumMember,EnumMembers
EnumMember:EnumMemberAttributesoptIdentifierEnumMemberAttributesoptIdentifier=AssignExpression
EnumMemberAttributes:EnumMemberAttributeEnumMemberAttributeEnumMemberAttributes
EnumMemberAttribute:DeprecatedAttributeUserDefinedAttribute@disable
AnonymousEnumDeclaration:enum:EnumBaseType{EnumMembers}enum{AnonymousEnumMembers}
AnonymousEnumMembers:AnonymousEnumMemberAnonymousEnumMember,AnonymousEnumMember,AnonymousEnumMembers
AnonymousEnumMember:EnumMemberEnumMemberAttributesoptTypeIdentifier=AssignExpression

Enum declarations are used to define a group of constants.

Named Enums

Named enums are used to declare related constants and group them by giving them a unique type. TheEnumMembers are declared in the scope of the named enum. The named enum declares a new type, and all theEnumMembers have that type.

This defines a new typeX which has valuesX.A = 0,X.B = 1,X.C = 2:

enum X { A, B, C }// named enum

A variable can be of named enum type. Its default initializer is the first member defined for the enum type.

enum X { A = 3, B, C }X x;assert(x == X.A);x |= X.B;assert(x & X.A);

The result type of a binary operation on a named enum is definedhere.

If theEnumBaseType is not explicitly set, and the firstEnumMember has anAssignExpression, it is set to the type of thatAssignExpression. Otherwise, it defaults to typeint.

enum Foo { E }Foo f;int i;i = f;// OKf = i;// errorf =cast(Foo)i;// OKf = 0;// errorf = Foo.E;// OK

A named enum member cannot declare its ownType.

See also:final switch on a named enum.

Enum Member Values

The value of anEnumMember is given by itsAssignExpression if present. If there is noAssignExpression and it is the firstEnumMember, its value is converted toEnumBaseType from0. If there is noAssignExpression and it is not the firstEnumMember, it is given the value of the previousEnumMember + 1:

enum E :char{    a,    b =char.max,    c// overflow}staticassert(E.a == 0);

AllEnumMembers are in scope for theAssignExpressions.

enum A = 3;enum B{    A = A// error, circular reference}enum C{    A = B,// A = 4    B = D,// B = 4    C = 3,// C = 3    D// D = 4}enum E : C{    E1 = C.D,    E2// error, C.D is C.max}

An empty enum body signifies an opaque enum - the enum members are unknown.

enum X;// opaque enumwriteln(X.init);// error: enum X is opaque and has no default initializer

Enum Properties

Enum properties only exist for named enums.

Named Enum Properties
.initFirst enum member value
.minSmallest enum member value
.maxLargest enum member value
.sizeofSize of storage for an enumerated value

For example:

enum X { A = 3, B = 1, C = 4, D, E = 2 }X.init// is X.AX.min// is X.BX.max// is X.DX.sizeof// is same as int.sizeof

TheEnumBaseType of named enums must support comparison in order to compute the.max and.min properties.

Enum Copying and Assignment

A named enum type never has acopy constructor,postblit, oridentity assignment overload, even if one is defined by itsEnumBaseType.

When copying a named enum value whose base type is astruct with a copy constructor, the copy constructor is not called:

struct S{this(ref S rhs) {assert(0); }}enum E : S { A = S.init }void main(){    E e1;    E e2 = e1;// ok - copy constructor not called}

When copying a named enum value whose base type is astruct with a postblit, the postblit is not called:

struct S{this(this) {assert(0); }}enum E : S { A = S.init }void main(){    E e1;    E e2 = e1;// ok - postblit not called}

When assigning a named enum value to another object of the same type, if the base type of those values is astruct with an identity assignment overload, the identity assignment overload is not called:

struct S{void opAssign(S rhs) {assert(0); }}enum E : S { A = S.init }void main(){    E e1, e2;    e2 = e1;// ok - opAssign not called}

Anonymous Enums

If the enumIdentifier is not present, then the enum is ananonymous enum, and theEnumMembers are declared in the scope theEnumDeclaration appears in. No new type is created.

TheEnumMembers can have different types. Those types are given by the first of:

  1. TheType, if present. Types are not permitted when anEnumBaseType is present.
  2. TheEnumBaseType, if present.
  3. The type of theAssignExpression, if present.
  4. The type of the previousEnumMember, if present.
  5. int
enum { A, B, C }// anonymous enum

Defines the constantsA = 0,B = 1,C = 2, all of typeint.

Enums must have at least one member.

The value of anEnumMember is given by itsAssignExpression if present. If there is noAssignExpression and it is the firstEnumMember, its value is the.init property of theEnumMember's type. If there is noAssignExpression and it is not the firstEnumMember, it is given the value of the previousEnumMember + 1:

AllEnumMembers are in scope for theAssignExpressions.

enum { A, B = 5 + 7, C, D = 8 + C, E }

SetsA = 0,B = 12,C = 13,D = 21, andE = 22, all of typeint.

enum :long { A = 3, B }

SetsA = 3 andB = 4, both of typelong.

enum : string{    A ="hello",    B ="betty",    C// error, cannot add 1 to "betty"}
enum{    A = 1.2f,// A is 1.2f of type float    B,// B is 2.2f of type floatint C = 3,// C is 3 of type int    D// D is 4 of type int}

Single Member Syntax

If there is only one member of an anonymous enum, the{ } can be omitted. Gramatically, this is a form ofVarDeclarations.

enum i = 4;// i is 4 of type intenumlong l = 3;// l is 3 of type long

This is also known as amanifest constant.

Manifest Constants

Enum members are manifest constants, which exist only at compile-time.

Manifest constants are not lvalues, meaning their address cannot be taken. They exist only in the memory of the compiler.

enum size =__traits(classInstanceSize, Foo);// evaluated at compile-time

The initializer for a manifest constant is evaluated using compile-time function evaluation.

template Foo(T){// Not bad, but the 'size' variable will be located in the executable.const size_t size = T.sizeof;// evaluated at compile-time// ... use of 'size' at compile-time ...}template Bar(T){// Better, the manifest constant has no runtime location in the executable.enum size_t size = T.sizeof;// evaluated at compile-time// ... use of 'size' at compile time ...// Taking the address of Foo!T.size also causes it to go into the exe file.auto p = &Foo!T.size;}
Interfaces
Type Qualifiers
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Fri Feb 20 21:32:34 2026

[8]ページ先頭

©2009-2026 Movatter.jp