File: stronglytyped.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (56 lines) | stat: -rw-r--r-- 1,976 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Enumeration values in bf(C++) are in fact tt(int) values, thereby bypassing
type safety. E.g., values of different enumeration types may  be
compared for (in)equality, albeit through a (static) type cast.

Another problem with the current tt(enum) type is that their values are not
restricted to the enum type name itself, but to the scope where the
enumeration is defined. As a consequence, two enumerations having the same
scope cannot have identical names.

Such problems are solved by defining
    em(enum classes). An emi(enum class) can be defined as in the following
example:
        verb(    enum class SafeEnum
    {
        NOT_OK,     // 0, by implication
        OK          = 10,
        MAYBE_OK    // 11, by implication
    };)

Enum classes use tt(int) values by default, but the used value type can
easily be changed using the tt(: type) notation, as in:
        verb(    enum class CharEnum: unsigned char
    {
        NOT_OK,
        OK
    };)

To use a value defined in an enum class its enumeration name must be
provided as well. E.g., tt(OK) is not defined, tt(CharEnum::OK) is.

    Using the data type specification (noting that it defaults to tt(int)) it
is possible to use enum class forward declarations.
        hi(forward declaration: enum class)
    E.g.,
        verb(    enum Enum1;                 // Illegal: no size available
    enum Enum2: unsigned int;   // Legal: explicitly declared type

    enum class Enum3;           // Legal: default int type is used
    enum class Enum4: char;     // Legal: explicitly declared type)

A sequence of symbols of a strongly typed enumeration can also be
indicated in a tt(switch) using the i(ellipsis) syntax, as shown in the next
example:
        verb(    SafeEnum enumValue();

    switch (enumValue())
    {
        case SafeEnum::NOT_OK ... SafeEnum::OK:
            cout << "Status is known\n";
        break;

        default: 
            cout << "Status unknown\n";
        break;
    })