Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22248

Initializing enum-indexed array?

$
0
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?


Viewing all articles
Browse latest Browse all 22248

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>