#include // sout #include // ato #include // integral enum Letter { A = 'A', B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, a = 'a', b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z }; enum( Letter ) Greek { Alph = A, Beta = B, Gamma = G, /* more enums */ Zeta = Z }; // alphabet intersection // integral enum( char ) Currency { Dollar = '$', Cent = '¢', Yen = '¥', Pound = '£', Euro = 'E' }; // iso-latin-1 enum( Currency ) Europe { Euro = Currency.Euro, Pound = Currency.Pound }; enum( signed char ) srgb { Red = -1, Green = 0, Blue = 1 }; enum( long long int ) BigNum { X = 123_456_789_012_345, Y = 345_012_789_456_123 }; // non-integral enum( double ) Math { PI_2 = 1.570796, PI = 3.141597, E = 2.718282 }; enum( _Complex ) Plane { X = 1.5+3.4i, Y = 7+3i, Z = 0+0.5i }; // pointer enum( const char * ) Name { Fred = "FRED", Mary = "MARY", Jane = "JANE" }; int i, j, k; enum( int * ) ptr { I = &i, J = &j, K = &k }; //enum( int & ) ref { I = i, J = j, K = k }; // tuple //enum( [int, int] ) { T = [ 1, 2 ] }; // function void f() {} void g() {} enum( void (*)() ) funs { F = f, G = g }; // aggregate struct Person { char * name; int age, height; }; enum( Person ) friends { Liz = { "ELIZABETH", 22, 170 }, //Beth = Liz, Jon = { "Jonathan", 35, 190 } }; enum() Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND }; Mode mode = O_RDONLY; void opaque() { bool b = mode == O_RDONLY || mode < O_APPEND; // disallowed //int www = mode; // disallowed } enum( char * ) Colour { Red = "red", Green = "green", Blue = "blue" }; enum E1 { A1, B1, C1 = A1, D1 = B1 }; enum(float) E2 { A2 = 3.5, B2 = 4.5, C2 = A, D2 = B }; void fred() { Letter letter = A; Greek greek = Beta; letter = Beta; // allowed, letter == B //greek = A; // disallowed for ( Greek l = Alph; posn(l) < posn(Gamma); l = succ( l ) ) { printf( "%s %c %d\n", label( l ), value( l ), posn( l ) ); } for ( Currency c = Dollar; posn(c) < posn(Currency.Euro); c = succ( c ) ) { printf( "%s %c %d\n", label( c ), value( c ), posn( c ) ); } } enum( const char * ) Names { Fred = "FRED", Mary = "MARY", Jane = "JANE" }; enum( const char * ) Names2 { inline Names, Jack = "JACK", Jill = "JILL" }; enum( const char * ) Names3 { inline Names2, Sue = "SUE", Tom = "TOM" }; void bar() { Names fred = Names.Fred; (Names2)fred; (Names3)fred; (Names3)Names2.Jack; // cast to super type Names2 fred2 = fred; Names3 fred3 = fred2; // assign to super type const char * name = fred; Names name = Fred; sout | name | label( name ) | posn( name ) | value( name ); } void f( Names n ) { sout | "Name" | posn( n ); } void g( Names2 ); void h( Names3 ); void j( char * ); enum CColour { Red, Blue, Green }; CColour c0 = 0; CColour c1 = 1; CColour c = 2; int w = Red; void coo() { enum(int) Color { Red, Blue, Green }; Colour c = Red; sout | countof( Colour ) | Countof( c ); // sout | Countof( Colour ); sout | countof( c ); } // enum(int) Week ! { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun }; // enum(int) RGB ! { Red, Green, Blue }; // void foo() { // with ( Week, RGB ) { // weekday = Sun; // rgb = Green; // } // } void baz() { enum(int) Count { First, Second, Third/* = First*/, Fourth/* = Second*/ }; enum CCount { First, Second, Third/* = First*/, Fourth/* = Second*/ }; Count cnt = Second; CCount ccnt = Second; if ( cnt < Third ) sout | "less than Third"; if ( cnt ) sout | "XXX"; if ( ccnt ) sout | "YYY"; enum(float) F {WWW = 0.0}; F f; if ( f ) sout | "FFF"; bool ?!=?( Name n, zero_t ) { sout | "DDD"; return n != Fred; } Name n = Mary; if ( n ) sout | "NAME"; choose( cnt ) { case First: sout | "First"; case Second: sout | "Second"; case Third: sout | "Third"; case Fourth: sout | "Fourth"; } // for (d; Week) { sout | d; } // for (p; +~=Planet) { sout | p; } for ( cx; Count ) { sout | cx | nonl; } sout | nl; for ( cx; +~= Count ) { sout | cx | nonl; } sout | nl; for ( cx; -~= Count ) { sout | cx | nonl; } sout | nl; for ( Count cx = lowerBound();; ) { sout | cx | nonl; if ( cx == upperBound() ) break; cx = succ( cx ); } sout | nl; } int main() { fred(); Names name = Names.Fred; // f( name ); int jane_pos = posn( Names.Jane ); const char * jane_value = value( Names.Jane ); const char * jane_label = label( Names.Jane ); sout | Names.Jane | posn( Names.Jane) | label( Names.Jane ) | value( Names.Jane ); bar(); baz(); coo(); enum Ex { Ax, Bx, Cx, Nx }; float H1[Nx] = { [Ax] : 3.4, [Bx] : 7.1, [Cx] : 0.01 }; // C // float H2[Ex] = { [Ax] : 3.4, [Bx] : 7.1, [Cx] : 0.01 }; // CFA enum(int) E { A = 3 } e = A; sout | A | label( A ) | posn( A ) | value( A ); sout | e | label( e ) | posn( e ) | value( e ); }