C preprocessor macros: check if token was declared?

I don't think BOOST_PP_SEQ_CONTAINS can be implemented. It would require you to be able to compare two sequences of preprocessing tokens, which you can't do.

I don't think BOOST_PP_SEQ_CONTAINS can be implemented. It would require you to be able to compare two sequences of preprocessing tokens, which you can't do. However, if you rearrange your logic a bit, you can get something closer to what you want.

First, we need a couple of helper macros for use with BOOST_PP_SEQ_FOR_EACH: #include // General purpose macros: #define EXPAND_ENUM_CASE_2(text1, text2) text1 ## text2 #define EXPAND_ENUM_CASE(r, data, elem) \ case elem : EXPAND_ENUM_CASE_2(data ## _ ## CASE ## _ , elem) We can define the list of enumerators and the enumeration just as you have in the original question: #define WORKDAY_ENUMERATORS (Monday)(Tuesday)(Wednesday)(Thursday) enum Workday { BOOST_PP_SEQ_ENUM(WORKDAY_ENUMERATORS) }; As you can see, I've left Friday off of the list because no one actually does work on Friday. Let's consider as an example a function that returns some text describing the day of the week. Instead of testing whether an enumerator was included in the list, we define the cases for each of the values using macros: #define WORKDAY_CASE_Monday { return "Mondays suck"; } #define WORKDAY_CASE_Tuesday { return "Tuesdays are better than Mondays"; } #define WORKDAY_CASE_Wednesday { return "Hooray for humpday!"; } #define WORKDAY_CASE_Thursday { return "Thursdays are okay"; } #define WORKDAY_CASE_Friday { return "No one really works on Friday"; } We then generate the correct case statements for the list by using the WORKDAY_ENUMERATORS and concatenating the enumerators with the WORKDAY_CASE_ prefix: const char* get_day_text(Workday d) { switch (d) { BOOST_PP_SEQ_FOR_EACH(EXPAND_ENUM_CASE, WORKDAY, WORKDAY_ENUMERATORS) } return "WTF?!

That's not a workday!"; } If a day was not included in the WORKDAY_ENUMERATORS list, no case will be generated for it. Because we should be polite when we use the preprocessor, we then undefine the macros we used: #undef WORKDAY_CASE_Monday #undef WORKDAY_CASE_Tuesday #undef WORKDAY_CASE_Wednesday #undef WORKDAY_CASE_Thursday #undef WORKDAY_CASE_Friday I think this is kind of ugly, but it's one way to get almost the results you are seeking.

Thank you, this is a very good solution for my problem. – Thomas Sep 5 '10 at 18:24 Do you also know how to get Boost output that starts a new line for every case? – Thomas Sep 5 '10 at 18:28 @Thomas: No, but I'm not sure why that would matter: newlines are not significant after the evaluation of preprocessing directives.

– James McNellis Sep 5 '10 at 18:30 That could help me when gcc prints out errors (an error like "expected identifier before '('" could be hard to spot when the line is 20000 characters long) – Thomas Sep 5 '10 at 18:36.

You can't. The C preprocessor doesn't "understand" the C programming language, it just tokenizes it. It doesn't know what "enum" actually means.

The compiler handles that. If you want to test something in the preprocessor, then you'll have to provide preprocessor macros for it to use. Edit: sorry, missed that you were intending to use Boost.Preprocessor.

I don't know whether that can provide the necessary macros, or not, once you've involved something from Boost in the definition of your enum.

Just don't use enum. It serves no useful purpose. Declare all your constants with #define, and use #ifdef.

Even in plain C, there is one great advantage of enum over #define: constants defined with enum are known to the debugger, but constants defined with #define usually aren't. Less important but still useful is that the compiler has enough information to yell at you if you forget to include some of the values of an enum in a switch statement. In C++ you also get the benefit of the constants actually being of their own type.

– Zack Sep 5 '10 at 19:41 I'll give you that, but it's still an implementation issue. If the compiler and debugger implementation were sufficiently smart, they could be aware of #define just like enum. On the other hand, as far as the actual C language is concerned, #define is strictly more powerful than enum.

There are useful things you can do with #define which can't be done with enum, but there's nothing (in the bounds of the actual language, not debuggers or implementation-specific things) you can accomplish with enum that you couldn't do with #define. – R.. Sep 5 '10 at 20:02 I'm coding in c++ and put my enums in classes or namespaces, which is not possible with define. – Thomas Sep 5 '10 at 20:10 OK, you have a good reason to use enum then.

I just wish everyone would stop tagging C++ questions as C pretending they're the same language... – R.. Sep 5 '10 at 23:03 There's one thing that you can do with enum constants that you can't do with #define - have a struct or union tag and/or member with the same name. – caf Sep 5 '107 at 0:36.

One approach is to have a great big #define or ". H" file which covers all your weekdays (the . H file has the advantage that you don't need to end all likes with backslash) and includes all the relevant information for them in macros.

Then #define the generator macro to do something, invoke the big macro (or #include the header), #undef the generator macro and define it to do something else, invoke the big macro again, etc. In this scenario, one variation of the generator macro would generate something like "case ENUM_foo: func_foo(); break;". You could then write all the appropriate code for the appropriate func_* functions and they would be called as appropriate.

This sounds like James' suggestion to me. – Thomas Sep 5 '10 at 20:24.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions