问题描述
枚举i = 2有什么区别?
enum s =Hello;
和
immutable i = 2;
immutable s =Hello;
在D 2.0?
枚举
是用户定义的类型,而不是变量。 枚举e = 2;
是一个
短的东西像这样的枚举:int {e = 2}
(即一个匿名
枚举一个成员 e
),请参见。
根据定义,匿名枚举的所有成员将被放置到当前的
范围中。因此, e
是放置在当前作用域中的类型成员,其行为为
,如。另一方面,
immutable i = 2;
实际上创建一个变量 i
类型为int
这种差异有以下几个后果:
-
枚举e
将没有内存位置,没有地址(不是左值),因为
既不是类型也不是其成员有地址。即你不能像auto ptr =& e;
(就像你不能做auto ptr =& 2;
)。immutable
另一方面是一个正常的变量(只是不可变的)。
i - 所讨论的,
可变变量可以在编译时或运行时初始化,
,而
编译时必须知道一个类型(其所有成员定义类型)。 - 编译器可以简单地替换所有出现的
e
与2
。对于我
它
通常必须创建一个内存位置(尽管优化编译器
可能有时可以避免这种情况)。因此,对于枚举
的
编译期间的工作负载可能会稍低一点,而
二进制值稍微小些。 - 数组有一个惊人的区别。对于
枚举uint [2] E = [0,1];
和
immutable uint [2] I = [0,1] ;
访问枚举
,例如E [0]
,可以
比immutable
数组的数量级更慢。I [0]
,
特别是数组E
和code>变大。这是因为对于
immutable
数组,它只是一个正常的数组查找,例如一个全局
变量。对于枚举
,但是它看起来像在数组被使用之前每
创建一个数组。在一个全局枚举的函数内部
(不要
问我,为什么,但是编译器真的似乎只是用
替换外观在这种情况下也是如此)。我从来没有尝试,但会猜测
同样适用于枚举
字符串和其他非平凡类型。
枚举
,除非这些常量是数组或我需要一个内存位置,其他原因。
What's the difference between
enum i = 2;
enum s = "Hello";
and
immutable i = 2;
immutable s = "Hello";
in D 2.0?
An enum
is a user-defined type, not a variable. enum e = 2;
is ashort-hand for something like this enum : int { e = 2 }
(i.e. an anonymousenum with one member e
), see the documentation.By definition, all members of an anonymous enum are placed into the currentscope. So, e
is a type member placed into the current scope, where it behaveslike a literal.immutable i = 2;
on the other hand actually creates a variable i
of type int.
This difference has a couple of consequences:
enum e
will have no memory location and no address (is no lvalue), sinceneither a type nor its members have an address. I.e. you cannot do somethinglikeauto ptr = &e;
(just like you cannot doauto ptr = &2;
).immutablei
on the other hand is a normal variable (just immutable).- As discussed by Jonathan,immutable variables can be initialized at compile time or at run-time,whereas a type (with all its members defining the type) must be known atcompile time.
- The compiler can simply replace all appearances of
e
with2
. Fori
itusually has to create a memory location (although an optimizing compilermight be able to avoid this sometimes). For this reason, the workload duringcompilation for anenum
might be expected to be somewhat lower, and thebinary somewhat smaller. - There is a surprising difference for arrays. For
enum uint[2] E = [0, 1];
andimmutable uint[2] I = [0, 1];
the access to theenum
, e.g.E[0]
, canbe orders of magnitude slower than for theimmutable
array, e.g.I[0]
,especially as the arraysE
andI
get bigger. This is so because for animmutable
array, it is just a normal array lookup to, say, a globalvariable. For theenum
however it looks like the array gets created everytime before it gets used, e.g. inside a function for a globalenum
(don'task me, why, but the compiler really seems to simply replace the appearancewith the value in this case, too). I have never tried but would guess thatthe same applies toenum
strings and other non-trivial types.
To sum up: when I use compile-time constants, I usually take enum
unlessthose constants are arrays or I need a memory location for some other reason.
这篇关于枚举vs不可变D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!