Pragma in define macro

Anycorn picture Anycorn · Jun 12, 2010 · Viewed 42.1k times · Source

Is there some way to embed pragma statement in macro with other statements?

I am trying to achieve something like:

#define DEFINE_DELETE_OBJECT(type)                      \
    void delete_ ## type_(int handle);                  \
    void delete_ ## type(int handle);                                                \
    #pragma weak delete_ ## type_ = delete_ ## type

I am okay with boost solutions (save for wave) if one exists.

Answer

Scott Wales picture Scott Wales · Jun 13, 2010

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
 ;