Pragma in define macro

If you’re using c99 or c++0x there is the pragma operator, used as

_Pragma("argument")

which is equivalent to

#pragma argument

except it can be used in macros (see section 6.10.9 of the c99 standard, or 16.9 of the c++0x final committee draft)

For example,

#define STRINGIFY(a) #a
#define DEFINE_DELETE_OBJECT(type)                      \
    void delete_ ## type ## _(int handle);                  \
    void delete_ ## type(int handle);                   \
    _Pragma( STRINGIFY( weak delete_ ## type ## _ = delete_ ## type) )
DEFINE_DELETE_OBJECT(foo);

when put into gcc -E gives

void delete_foo_(int handle); void delete_foo(int handle);
#pragma weak delete_foo_ = delete_foo
 ;

Leave a Comment