cppannotations/yo/first/stronglytyped.yo
Frank B. Brokken ab2875425a WIP
git-svn-id: https://cppannotations.svn.sourceforge.net/svnroot/cppannotations/trunk@256 f6dd340e-d3f9-0310-b409-bdd246841980
2009-10-16 14:37:56 +00:00

43 lines
1.6 KiB
Text

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 values.
In the C++0x standard these 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 in C++0x: explicitly declared type
enum class Enum3; // Legal in C++0x: default int type is used
enum class Enum4: char; // Legal in C++0x: explicitly declared type
)