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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:16:06+00:00 2026-05-26T21:16:06+00:00

gcc has a very nice extension in C that allows you to keep data

  • 0

gcc has a very nice extension in C that allows you to keep data in arrays using enum as keys:

 enum keys
 {
      key_alpha = 0,
      key_beta = 1,
      key_gamma = 2
 };

 ValType values = 
 {
      [ key_alpha ] = { 0x03b1,"alpha" },
      [ key_gamma ] = { 0x03b3,"gamma" },
      [ key_beta ]  = { 0x03b2,"beta" }
 };

This is nice because if the list has to change, adding or removing a line doesn’t destroy the assignment, it is obvious which key corresponds to which value, and results in simple code no different from plain standard array initialization.

Unfortunately, this extension is not available in g++.

What would be the preferred lightweight way of doing the same thing in C++? Preferably something not based on <map> and the likes that use string keys, hidden indices, heavy templates or other CPU- and memory-heavy stuff?

  • 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-26T21:16:06+00:00Added an answer on May 26, 2026 at 9:16 pm
    #include <iostream>
    
    #define KEYS_DEF \
        KEY_DEF( alpha, 0x03b1, "alpha" ),  \
        KEY_DEF( beta,  0x03b2, "beta" ),   \
        KEY_DEF( gamma, 0x03b3, "gamma" )
    
    #define KEY_DEF( identifier, id, name )  identifier
    enum keys { KEYS_DEF };
    
    #undef KEY_DEF
    #define KEY_DEF( identifier, id, name )  { id, name }
    struct ValType { int id; char const* name; };
    ValType const values[] = { KEYS_DEF };
    
    int main()
    {
        using namespace std;
        for( int i = alpha;  i <= gamma;  ++i )
        {
            cout << values[i].name << endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc 4.4.2 c89 I have a function that has to run (config_relays). It make
Has anyone taken advantage of the automatic vectorization that gcc can do? In the
I have a c-library which I use in gcc. The library has the extension
We are compiling an application that uses OpenMP. We are using gcc 4.4, with
I am using GCC to build my embedded C application, that I debug with
I am reposting a comp.std.c++ Usenet discussion here because that group has become very
GCC has a very verbose format for certain template error messages: ... some_class<A,B,C> [with
I've recently been very interested in compilers and how they work. Since gcc has
I am very interested in some studies or empirical data that shows a comparison
Has anybody added a pass to gcc ? or not really a pass but

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.