# macro operator#<token>Prefixing a macro token with # will quote that macro token. This allows you to turn bare words in your source code into text tokens. This can be particularly useful for writing a macro to convert an enum into a string:
enum ColorType
{
CT_RED,
CT_GREEN,
CT_BLUE
};
// note the use of CT to stand for the enum value
// and #CT to create a string, such as "CT_RED"
#define CONV_CASE(CT) case CT: return #CT
std::string getString (ColorType t)
{
switch ( t )
{
CONV_CASE( CT_RED );
CONV_CASE( CT_GREEN );
CONV_CASE( CT_BLUE );
default: return "unknown";
}
}
Related #define C preprocessor tutorial |