Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the use of previously defined members as part of later members in an enum definition legal?

namespace ValueType {
  enum Enum {
    Boolean = 0,
    Float = 1,
    Double,
    SInt = 8,
    SLong,
    UInt = SInt + (1 <<4),
    ULong = SLong + (1 << 4)
  };
}
like image 962
Geoff Avatar asked Oct 26 '09 21:10

Geoff


People also ask

How many enum members can have the same name?

The body of an enum type declaration defines zero or more enum members, which are the named constants of the enum type. No two enum members can have the same name.

What is a literal enum member?

A literal enum member is a constant enum member with no initialized value, or with values that are initialized to. any string literal (e.g. "foo", "bar, "baz") any numeric literal (e.g. 1, 100) a unary minus applied to any numeric literal (e.g. -1, -100)

How to get the associated value of an enum member?

If the declaration of the enum member has no initializer, its associated value is set implicitly, as follows: 1 If the enum member is the first enum member declared in the enum type, its associated value is zero. 2 Otherwise, the associated value of the enum member is obtained by increasing the associated value of the textually... More ...

What types can be declared in an enum?

An enum declaration may explicitly declare an underlying type of byte, sbyte, short, ushort, int, uint, long or ulong. Note that char cannot be used as an underlying type.


1 Answers

Yes -- the requirement is that it's an integral constant expression. The C++ standard includes the following example:

enum { d, e, f=e+2 };
like image 89
Jerry Coffin Avatar answered Oct 20 '22 06:10

Jerry Coffin