Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1048903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:31:51+00:00 2026-05-16T16:31:51+00:00

This is for the C preprocessor experts: How can I declare an enum with

  • 0

This is for the C preprocessor experts:

How can I declare an enum with a list of some identifiers and later during the switch-statement check if an identifier was included in the list?

Example of what I need:

typedef enum { e1, e2, e3, e4, e5, e6 } e;

e x;
switch (x) {
#if DECLARED_IN_ENUM (e1)
  case e1 : ...
#endif
  /* etc. */
}

I thought of using a Boost sequence and expanding it into a comma separated list in the enum, but how can I check later if the sequence contains a certain token?

EDIT: What I was able to do with Boost is:

#define e1 e1
#define e2 e2
#define e3 e3
#define e4 e4
#define e5 e5
#define e6 e6
#define E (e1)(e2)(e3)(e4)(e5)(e6)

typedef enum { BOOST_PP_SEQ_ENUM(E) } e;

e x;
switch (x) {
#if defined (e1)
  case e1 : ...
#endif
  /* etc. */
}

That is not very beautiful, and I would prefer something like:

#define E (e1)(e2)(e3)(e4)(e5)(e6)

typedef enum { BOOST_PP_SEQ_ENUM(E) } e;

e x;
switch (x) {
#if BOOST_PP_SEQ_CONTAINS (e1,E)
  case e1 : ...
#endif
  /* etc. */
}

but how could BOOST_PP_SEQ_CONTAINS be implemented?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:31:52+00:00Added an answer on May 16, 2026 at 4:31 pm

    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 <boost/preprocessor.hpp>
    
    // 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using C preprocessor one can stringify macro argument like this: #define TO_STRING(x) a
I'm trying to use preprocessor tricks to declare a magic variable. Something like this:
This is my sample code: SPWeb web = SPContext.Current.Web SPList list = web.Lists[TestList]; try
I have this preprocessor directive: #define INDEXES_PER_SECTOR BYTES_PER_SECTOR / 4 where BYTES_PER_SECTOR is declared
What's the closest GCC equivalent to this MSVC preprocessor code? #pragma warning( push )
I found this regarding how the C preprocessor should handle string literal concatenation (phase
Is this possible? Basically, I want to invoke the preprocessor as if I were
The preprocessor can be used to replace certain keywords with other words using #define
How can I define a preprocessor macro when using xcodebuild? I need to build
for(x;x<crap;x++) { macro(x,y); } How is this handled by preprocessor? Is this loop unrolled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.