Topic: Make (strongly typed) enums castable to char const * const


Author: Dominic Fandrey <kamikaze@bsdforen.de>
Date: Thu, 25 Aug 2016 15:47:29 +0200
Raw View
I searched this list for similar suggestions, but of course I may
have overlooked something.

I frequently use the following pattern:

=C2=A0 =C2=A0 enum class FooBar { KEKS, DOSE };
=C2=A0 =C2=A0 char const * const FooBarStr[]{"KEKS", "DOSE"};

I use the strings to in error messages, verbose output, etc.

=C2=A0 =C2=A0 FooBarStr[static_cast<int>(FooBar::KEKS)] // "KEKS"

This has some disadvantages, e.g. enums with explicitly stated values
may have gaps, negative values and may be defined out of order.
Also, changing the enum also means changing the strings manually.

What I'd like to see:

=C2=A0 =C2=A0 static_cast<char const *>(FooBar::KEKS) // "KEKS"

I think this would be simple to add to an existing compiler and it's
very unlikely to clash with existing code, because you have to jump
through some hoops to cast a strongly typed enum to a pointer type.

--=20
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


--=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/1472132849.17129.9.camel%40bsdforen.de.

.