Stringizing Operator, Charizing Operator and Token pasting operator.

29 05 2008

What ever C++ best practices says, its a bit tough to eliminate macros. Especially in extensible frameworks. The best example is MFC. Indeed macros have drawbacks, but its the best wepon to inject code inside client classes which uses the framework.

For instance, the MFC message map itself. The message map support is provided by three macros – DECLARE_MESSAGE_MAP(), BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP(). These macros declare static message map array and some utility functions to class. Indeed, the user is free from all those complications and everything are buried deep inside these macros .

There are some helpful operators which helps you to write powerful marcos. They are #, #@ and ##.

Stringizing Operator( # )
As name says – Stringizing Operator converts the macro parameter to a string. if # is prefixed before the parameter name, and used in the macro body, that parameter will get enclosed in double quotes. For instance see the following macro used for showing messagebox. The message – CDialogDlg::OnButton will get expanded to “CDialogDlg::OnButton”

MESSAGE_BOX( CDialogDlg::OnButton );
// The macro which shows Message.
#define MESSAGE_BOX( Message ) \
    AfxMessageBox( _T( #Message )); \

if you want to see a real world example, check the following line from DAOCORE.CPP of MFC. A DAO trace macro.

#define DAO_TRACE(f) AfxDaoTrace(f, #f, THIS_FILE, __LINE__)

Charizing Operator( #@ )
The Charizing operator is used to convert a macro parameter to a charector constant. If its prefixed in macro parameter, the parameter get enclosed in single quotes to form a char constant. For instance see the e.g. below. Its taken from MSDN.

// Same as a = 'b';
char a = MAKECHAR( b );
#define MAKECHAR( x )  #@x

Token-Pasting Operator (##)
Its also known as “Merging operator”. If its prefixed in macro parameter, that token just get pasted for each occurance. The best example is _T() macro. See its definition below.

// Assume unicode is defined.
// This will expand to csString = L"Hello";
CString csString = _T( "Hello" );
#define _T(x)       __T(x)
#define __T(x)      L ## x

Here the ##x pasts the token directly there. See one more example to see the power of token pasting operator. It can also be used to give unique names to the member variables in the injected code. See the definition of DECLARE_DYNAMIC() macro. Here the class name is pasted to generate unique name for the member variable injected.

#define DECLARE_DYNAMIC(class_name) \
protected: \
    static CRuntimeClass* PASCAL _GetBaseClass(); \
public: \
    static const AFX_DATA CRuntimeClass class##class_name; \
    virtual CRuntimeClass* GetRuntimeClass() const; \

But still keep in mind that macros are twin side sharp swords. If not properly used, … 😉

Targeted Audience – Intermediate.