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

  • Home
  • SEARCH
  • 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 7073623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:57:06+00:00 2026-05-28T05:57:06+00:00

I am writing a small library with which enums in C++ should get easier.

  • 0

I am writing a small library with which enums in C++ should get easier. The syntax is something like:

ENUM_START(MyEnum)
    ENUM_VAL(Val1)
    ENUM_VAL(Val2)
    ...
ENUM_END

This macros create a class MyEnum with allows for example the following access:

MyEnum bla=MyEnum::Val1;
for(MyEnum::iterator iter=MyEnum::begin();iter!=MyEnum::end();++iter)
    cout << iter->toString();

and a few more features like storing additional data (for example strings) to enum values.

The macros are finished and work, but are not as easy to define as shown above. For this I need a way to create a list of initializer functions with the ENUM_VAL macros that I later can call. Something like the following boost::mpl approach:

typedef mpl::vector<> list__COUNTER__;
#define ENUM_VAL(Name)                                                         \
    ...                                                                        \
    struct Init##Name{void init() {initialization code}};                      \
    typedef mpl::push_back<                                                    \
        list##(__COUNTER-1),                                                   \
        Init##Name                                                             \
      >::type list##__COUNTER__;                                               \

This way list##(__COUNTER__-1) contains in the end the type Init##Name and I can in the end call init() on all stored types using mpl foreach.

The problem now is the naming. I have to use __COUNTER__ twice per macro instantiation, which increments the counter twice. I already searched and found out that

  • The C preprocessor doesn’t calculate (__COUNTER__-1) when naming the variables
  • there is no way to read __COUNTER__ without incrementing it.

So I need another way to collect a list of functions I later can call.

  • 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-28T05:57:07+00:00Added an answer on May 28, 2026 at 5:57 am

    You could use Boost.Preprocessor and specifically it’s sequences. This would result in a macro-usage similar to this:

    MAKE_ENUM(MyEnum, (Val1)(Val2)(Val3))
    

    Using SEQ_ENUM, SEQ_FOR_EACH_I et al, you can then generate the code relatively easy.

    There was also a proposed Boost.Enum which might suit your needs already.

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

Sidebar

Related Questions

Is there any way of writing pexpect like small program which can launch a
Purpose I'm writing a small library for which portability is the biggest concern. It
I'm writing a small benchmarking library in C which is used to benchmark single
I am writing a small library, and in which I need to access several
all. I'm pretty new to C++, and I'm writing a small library (mostly for
I am writing a small matrix library in C++ for matrix operations. However, my
I'm writing a small and inadequate linear algebra library in C++ for a project
I'm writing a small tool in C# which will need to send and receive
I am writing a small I/O library to assist with a larger (hobby) project.
I'm writing a small Ruby command-line application that uses fileutils from the standard library

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.