Click to See Complete Forum and Search --> : I want to acheive this


MyName
October 30th, 2002, 10:46 AM
I want to acheive the following
void ab()
{
}

#define X1 a
#define X2 b
#define X X1##X2
X(); // So I want this to be equivalent to calling ab()


any ideas how i can acheive that ?

Paul McKenzie
October 30th, 2002, 11:29 AM
Why not just call ab()?

Regards,

Paul McKenzie

Dmitry Zemskov
October 30th, 2002, 01:41 PM
// Helper macro _CONCAT:
// The following piece of macro magic joins the two
// arguments together, even when one of the arguments is
// itself a macro (see 16.3.1 in C++ standard). The key
// is that macro expansion of macro arguments does not
// occur in _CONCAT2 but does in _CONCAT1.

#define _CONCAT( _HEAD, _TAIL ) _CONCAT1( _HEAD, _TAIL )
#define _CONCAT1( _HEAD, _TAIL ) _CONCAT2( _HEAD, _TAIL )
#define _CONCAT2( _HEAD, _TAIL ) _HEAD##_TAIL

Now if you use the macro _CONCAT like this:

#define X1 a
#define X2 b
_CONCAT( X1, X2 )

you will get 'ab' after prefrocessing.

BTW: You can add /P option in Project Settings\C/C++\General\Project Options to get preprocessed output in MSVC Dev Studio.

MyName
October 30th, 2002, 02:10 PM
you are the man