strenum /

is a macro I wrote for C++ which I've found really useful. It creates things which act just like an enum, but where the value names are inside the scope of the enum.

To avoid values in different enums having the same name, a traditional coding style is to write something like this:

enum Answer
{
    Answer_Right,
    Answer_Wrong,
    Answer_Pending,
};

A naming convention like this is necessary to distinguish from a Direction_Right, or a function that might be called Pending(). The way I wish the language worked is to put the members of an enum under the scope of the enum, so that you'd be able to write something like Answer::Right.

//strenum.h
 
#define strenum( name ) \
struct name { enum name##_e; inline operator name##_e&() \
{ return name##_d; } inline operator const name##_e&() const \
{ return name##_d; } name() {} name( const name##_e& i ) : \
name##_d(i) {} name( const int& i ) : name##_d(name##_e(i)) \
{} private: name##_e name##_d; }; enum name::name##_e

This is very ugly code, but to work with it I just put it in a header file and forget what it looks like. It will generate a struct which has an enum embedded in it, and which has acts as an interface to that enum.

strenum( Material )
{
    Wood = 17,
    Metal,
    Tin = Material::Metal,
};
 
Material a = Material::Wood;
Material b( a );

Intellisense /

is the main practical advantage of this. I can now forget the names of all my enum values and use Intellisense. (There are a few extra items in this Intellisense view that I'd rather didn't show up, but it does the job).

Download /

strenum.h or copy and paste from the box above.

Feel free to use it in any program you want.