Is typedef an alien?

Chad J chadjoan at __spam.is.bad__gmail.com
Fri Sep 25 00:38:45 PDT 2009


Hmmmm, there's a lot of hating on typedef.

I've found it rather useful for at least one thing:

typedef uint ColorFormat;
enum : ColorFormat
{
	RGB,
	RGBA,
	HSV,
	CMYK,
	// etc
}

Now you have an enum that is type safe but doesn't require a qualified
name to use.

void convertToFormat( SomeTextureType someTexture, ColorFormat fmt )
{
	// etc
}

void main()
{
	// ...
	auto foo = new SomeTextureType(...);

	convertToFormat(foo, 1); // Error, 1 is not a ColorFormat
	convertToFormat(foo, ColorFormat.RGBA ); // Nope*
	convertToFormat(foo, RGBA); // This works.
}

* It is possible to declare named enums that are also typesafe but
require the enum member to be qualified by the enum's name as in this
line.  Such enums are declared like so:

enum ColorFormat : uint
{
// etc
}



More information about the Digitalmars-d mailing list