0

I want something in c++ that come as close as possible to that usage.

enum foo
{
    bar_0 = "hello",
    bar_1 = "world"
};
 //..
 cout << to_string(bar_0) << " " << to_string(bar_1);

with output "hello world".

I'm aware of solution that looks like this

ENUM foo
{
    bar_0 ,
    bar_1
};
 //..
 cout << to_string(bar_0) << " " << to_string(bar_1);
 //>bar_0 bar_1

and solutions like this

 eunm foo
{
    bar_0 ,
    bar_1
};

 std::string foo_to_string[] = { "hello", "world"}
//+ some static assert of the sizes

Is there something sweeter outside? I want something were the enum-value and the string is at the same line

P.S.: I'm accepting solutions containing some dark-side-cpp-macro-magic

EDIT

Why is this not a duplicate?

My question is different to the duplicate marked on, because i want custom strings and not the name of the enum value. One can argue, that i can change the name of the enum-value this does often not work. For example error-msg.

  enum Error_msg
  {
          you_are_using_internet_behind_a_proxy,
  };

No one want to use this kind of error-msg and enum-value name.

user1235183
  • 3,002
  • 1
  • 27
  • 66
  • 2
    Any solution is going to be *"fostering [its] own lookup-table"* - or something equivalent, so if you don't show the exact code you're "aware" of and discuss the problems with it in your intended usage, how's anyone here supposed to know what you might consider better? – Tony Delroy May 09 '16 at 06:47
  • How determined are you to do this with enum? Because enum *only* supports integral types. – kfsone May 09 '16 at 06:51
  • there are hundreds of ways to do it, and they are all more complicated than just writing a custom to_string function, which takes literally 5 minutes. – Richard Hodges May 09 '16 at 06:51
  • 1
    Does the [X Macro trick](http://www.drdobbs.com/cpp/the-x-macro/228700289) suffice? Or perhaps [this](http://stackoverflow.com/a/24876694/410767)? If not, please say why.... – Tony Delroy May 09 '16 at 07:01
  • X Macro trick works! – user1235183 May 09 '16 at 07:15

0 Answers0