Avoid Macro Extensions When Using Forced Preprocessor Sequences

I am trying to get the name of the OS and the compiler as a string in C ++. Although there are many questions about this, I did not find a definitive answer. So I tried using Boost.Predef 1.55, which defines macros like BOOST_OS_<OS>and BOOST_OS_<OS>_NAME.

Therefore, you can simply do if(BOOST_OS_<OS>) return BOOST_OS_<OS>_NAME;to support OS support. The same goes for compilers with COMPinstead OS. To avoid repetition, I wanted to use Boost.Preprocessor and put them in a loop.

I came up with the following:

#define MAKE_STMT_I2(PREFIX) if(PREFIX) return PREFIX ## _NAME;
#define MAKE_STMT_I(type, curName) MAKE_STMT_I2(BOOST_ ## type ## _ ## curName)
#define MAKE_STMT(s, type, curName) MAKE_STMT_I(type, curName)
#define OS_LIST (AIX)(AMIGAOS)(ANDROID)(BEOS)(BSD)(CYGWIN)(HPUX)(IRIX)(LINUX)(MACOS)(OS400)(QNX)(SOLARIS)(UNIX)(SVR4)(VMS)(WINDOWS)(BSDI)(DRAGONFLY)(BSD_FREE)(BSD_NET)(BSD_OPEN)

    BOOST_PP_SEQ_FOR_EACH(MAKE_STMT, OS, OS_LIST)

However, I run into problems when the values ​​expand soon. For instance. VMS already defines a macro with a name VMS, which is then replaced by OS_LIST. It #define OS_LIST (##AIX##)(##AMIGAOS##)(...doesn’t even help to do something , as it seems to expand in boost later.

How can I completely avoid the extension in the sequence?

+4
source share
2 answers

Since you rely on VMSan undefined token , a quick fix is ​​simple #undef VMS. Obviously, to avoid breaking up the code that relies on this macro, you should put your Boost PP code in your own .cpp file.

+2
source
How can I completely avoid the extension in the sequence?

. .

- boost :

1.

, MSalters.

, VMS , .

VMS undefined, , (MSalters ).

2.

2 , , :

#define OS_LIST (S_AIX)(S_BEOS)(S_VMS)

... MAKE_STMT; , :

#define MAKE_STMT_I2(PREFIX) if(PREFIX) return PREFIX ## _NAME;
#define MAKE_STMT_I(curName) MAKE_STMT_I2(BOOST_O ## curName)
#define MAKE_STMT(s, type, curName) MAKE_STMT_I(curName)
#define OS_LIST (S_AIX)(S_AMIGAOS)(S_ANDROID)(S_BEOS)(S_BSD)(S_CYGWIN)(S_HPUX)(S_IRIX)(S_LINUX)(S_MACOS)(S_OS400)(S_QNX)(S_SOLARIS)(S_UNIX)(S_SVR4)(S_VMS)(S_WINDOWS)(S_BSDI)(S_DRAGONFLY)(S_BSD_FREE)(S_BSD_NET)(S_BSD_OPEN)

(: , OS ).

, BOOST_OS_FOO BOOST_OS_FOO_NAME , , .

, S_FOO - . , , .

3.

:

#define OS_LIST (AIX)(BEOS)(8VMS)
#define BOOST_OS_8VMS BOOST_OS_VMS
#define BOOST_OS_8VMS_NAME BOOST_OS_VMS_NAME

, BOOST_OS_xxx/BOOST_OS_xxx_NAME, . , ( (pp-numbers), ).

+2

Source: https://habr.com/ru/post/1689695/


All Articles