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 8751307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:01:14+00:00 2026-06-13T13:01:14+00:00

Possible Duplicate: Enum to string : return the enum integer value if invalid /

  • 0

Possible Duplicate:
Enum to string : return the enum integer value if invalid / not found

In brief, the (working) definition code that I have is something like this:

enum Gadget
{
    First,
    Second,
}; 

const char* gadget_debug_names[] = {
    "First",
    "Second",
    // note: strings are same as enum tokens here, but bonus points if
    //       they can optionally be given different values
};

However, it’s error prone that the information is in multiple separate places that have to be maintained manually. (And in some cases in the code base I’m working with those two–or more–places aren’t even currently in the same file.) So it would be really nice to just name those things once.

Now we could do this with code generation and a declarative data file, but I’d prefer not to add another step to the existing build process if there’s a better way to do this. It would be perfect to have something that looks like

DEFINE_GADGET(First)
DEFINE_GADGET(Second)

(optionally with start/stop macros if needed) But, since macros are just plain text substitution, I can’t figure out any way to have the preprocessor “remember” the tokens while it’s writing out the enum definition.

I’ve thought that this might also be possible through meta-programming, but I’m at a loss on how to do it. All of the examples I’ve seen there involve recursively building a data structure. I can see how I might build the array of strings that way, but I’m not sure how I could pass in a token name, or how to build an enum. (And of course using metaprogramming just to build an array of strings would be pretty ridiculous.)

Is there any way for me to keep DRY here, without using code generation?

  • 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-06-13T13:01:15+00:00Added an answer on June 13, 2026 at 1:01 pm

    There is an old pre-processor trick for this:

    Gadget.data

    DEFINE_GADGET(First)
    DEFINE_GADGET(Second)
    

    Gadget.**

    #define QUOTE_VAL(X)  #X
    
    enum Gadget
    {
    #define DEFINE_GADGET(X)   X,
    #include "Gadget.data"
    #undef DEFINE_GADGET(X)
    }; 
    
    const char* gadget_debug_names[] = {
    #define DEFINE_GADGET(X)   QUOTE_VAL(X),
    #include "Gadget.data"
    #undef DEFINE_GADGET(X)
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java enum inheritance I'm working with a library that has an enumerated
Possible Duplicate: Java - Convert String to enum I have a method that uses
Possible Duplicate: Can’t create handler inside thread that has not called Looper.prepare() inside AsyncTask
Possible Duplicate: JSON serialization of c# enum as string I have two classes as
Possible Duplicate: Enum with strings Is it possible to have string constants in enum
Possible Duplicate: How do I Convert a string to an enum in C#? I
Possible Duplicate: Converting a string to an enumeration value in C# How do I
Possible Duplicate: C#: What does new() mean? I look at definition of Enum.TryParse: public
Possible Duplicate: How to enumerate an enum? Suppose that i have an enumeration like
Possible Duplicate: Using GetHashCode for getting Enum int value Is it safe to use

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.