Boost Preprocessor Question

Greetings, I was wondering if the following can be done with Boost Preprocessor. Given a list of preprocessor macro names, write a simple program that prints whether each macro is defined and its value if it is defined. In particular, I'd like to know if the following example program could be reduced/simplified with Boost: #include <iostream> using namespace std; #define STRING(x) #x #define STRINGIZE(x) STRING(x) #define IS " is " #define NOT "not " #define DEFINED "defined." #define VALUE " Its value is " extern int main () { #if defined __mips cout << STRING(__mips) IS DEFINED; cout << VALUE STRINGIZE(__mips) << endl; #else cout << STRING(__mips) IS NOT DEFINED << endl; #endif #if defined __unix cout << STRING(__unix) IS DEFINED; cout << VALUE STRINGIZE(__unix) << endl; #else cout << STRING(__unix) IS NOT DEFINED << endl; #endif #if defined __sgi cout << STRING(__sgi) IS DEFINED; cout << VALUE STRINGIZE(__sgi) << endl; #else cout << STRING(__sgi) IS NOT DEFINED << endl; #endif #if defined _COMPILER_VERSION cout << STRING(_COMPILER_VERSION) IS DEFINED; cout << VALUE STRINGIZE(_COMPILER_VERSION) << endl; #else cout << STRING(_COMPILER_VERSION) IS NOT DEFINED << endl; #endif #if defined __GNUC__ cout << STRING(__GNUC__) IS DEFINED; cout << VALUE STRINGIZE(__GNUC__) << endl; #else cout << STRING(__GNUC__) IS NOT DEFINED << endl; #endif return 0; } Thanks, Eric.

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Eric Lemings
Hi Jeff.
I was wondering if the following can be done with Boost Preprocessor. Given a list of preprocessor macro names, write a simple program that prints whether each macro is defined and its value if it is defined.
In particular, I'd like to know if the following example program could be reduced/simplified with Boost:
Not really. The problem is that you can't have macros expand to a use of the "defined" operator. You can condense it a bit: #include <iostream> inline void out(bool d, const char* n, const char* v) { if (d) { std::cout << n << " is defined as" << v << '\n'; } else { std::cout << n << " is not defined.\n"; } return; } #define STRING(x) #x #define BOTH(x) #x, STRING(: x) int main() { out( #if defined __mips 1 #endif + 0, BOTH(__mips)); out( #if defined __unix 1 #endif + 0, BOTH(__unix)); // ... return 0; } Note that, in current C++, you have to pass something else to the secondary STRING macro in BOTH in case x expands to nothing--to avoid an empty argument, which is undefined. (This will be allowed in C++0x when placemarkers ala C99 are adopted.) If you aren't worried about the (semi-pathological) case where a macro is defined as itself (e.g.) #define MACRO MACRO ...you can eliminate the preprocessor conditional and just do a runtime string comparison and make the listing really short. #include <iostream> #include <string> inline void out(const std::string& n, const std::string& v) { if ((": " + n) != v) { std::cout << n << " is defined as" << v << '\n'; } else { std::cout << n << " is not defined.\n"; } return; } #define STRING(x) #x #define OUT(x) out(#x, STRING(: x)); int main() { OUT(__mips) OUT(__unix) OUT(__sgi) // ... return 0; } Regards, Paul Mensonides
participants (2)
-
Eric Lemings
-
Paul Mensonides