enum class FooBar { KEKS = 2, DOSE = 2 }; FooBar fb = FooBar::KEKS;
cout << static_cast<char const*>(fb) ;Note that enums can have duplicate values, which throws a wrench into the reflection works. E.g.:enum class FooBar { KEKS = 2, DOSE = 2 };
What will the result be for:FooBar fb = FooBar::KEKS;
cout << static_cast<char const*>(fb) ;
On Thu, 2016-08-25 at 16:55 +0100, D. B. wrote:
> On Thu, Aug 25, 2016 at 4:17 PM, Derek Ross <antiq...@gmail.com> wrote:
>
> >
> > Note that enums can have duplicate values, which throws a wrench into the
> > reflection works. E.g.:
> >
> > enum class FooBar { KEKS = 2, DOSE = 2 };
> >
> > What will the result be for:
> >
> > FooBar fb = FooBar::KEKS;
> > cout << static_cast<char const*>(fb) ;
> >
> that only "throws a wrench into the reflection works" as proposed by the
> OP, by no means generally. the WG proposal linked (A) would not support
> this because it is static and (B) has presumably already accounted for such
> possibilities and effortlessly gets around them with compile-time
> wizardry... again, *presumably.*
>
It's not really useful for what I'm doing if it's not runtime capable.
On Thu, 2016-08-25 at 16:55 +0100, D. B. wrote:
> On Thu, Aug 25, 2016 at 4:17 PM, Derek Ross <antiq...@gmail.com> wrote:
>
> >
> > Note that enums can have duplicate values, which throws a wrench into the
> > reflection works. E.g.:
> >
> > enum class FooBar { KEKS = 2, DOSE = 2 };
> >
> > What will the result be for:
> >
> > FooBar fb = FooBar::KEKS;
> > cout << static_cast<char const*>(fb) ;
> >
> that only "throws a wrench into the reflection works" as proposed by the
> OP, by no means generally. the WG proposal linked (A) would not support
> this because it is static and (B) has presumably already accounted for such
> possibilities and effortlessly gets around them with compile-time
> wizardry... again, *presumably.*
>
It's not really useful for what I'm doing if it's not runtime capable.
For multiple mentions I'd just leave it implementation defined or
establish a simple first definition rule.
I am assuming that in most cases overlapping definitions are aliases:
enum class Flags { NONE = 0, COMPATIBLE = 1, COMPAT = 1, EXPERIMENTAL = 2 };
>> It's not really useful for what I'm doing if it's not runtime
>> capable.
>
> Then I am not an 'insider' and might well be wrong... but based on how
> static C++ is, I would not expect such a feature to arrive, or really
> any other dynamic features, beyond the 'necessary evils' of
> dynamic_cast/typeinfo.
Well, you'd use `get_enum_values` to obtain all declared values, use
some TMP to build an array of the corresponding names, and then look up
your run-time value in that array. Wrap into a function and you're good
to go. I presume that writing a generic solution for this (working for
all enumeration types) would be fairly straight-forward.
Anyway, I don't think that the fact that static reflection -- should it
ever enter the language -- *could* be used to solve a problem means that
we cannot include a feature *now* that does the same task in a more
accessible manner. (As I've said before, I'd prefer a library solution,
not `static_cast` magic.) If standard library implementors can
re-implement that feature as a thin wrapper around static reflection in
the future, that's fine. Looking up a human-readable name for an
enumeration constant is a reasonable thing a beginner C++ programmer
might want to do. Writing template meta-programs using static
reflection is not an option for them and still not very convenient for
those programmers who would in theory be able to do it.
--
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 email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/87vayosip3.fsf%40gmail.com.
El 26/8/2016 20:30, <szollos...@gmail.com> escribió:
>
> Hi,
>
> I hate to troll in (esp. since I'm top-posting :) ), but you know that you can achieve this using macro-metaprogrammed FOREACH(); or, if you're okay with uintptr_t values and don't care about enum values, you can use string addresses (when you put strings in a struct using a macro), which is way more readable?
Even better, x-macros https://en.wikipedia.org/wiki/X_Macro?wprov=sfla1
>
> Regards,
> -lorro
>
>
> 2016. augusztus 25., csütörtök 15:47:34 UTC+2 időpontban Kamikaze Dominic Fandrey a következőt írta:
>>
>> I searched this list for similar suggestions, but of course I may
>> have overlooked something.
>>
>> I frequently use the following pattern:
>>
>> enum class FooBar { KEKS, DOSE };
>> char const * const FooBarStr[]{"KEKS", "DOSE"};
>>
>> I use the strings to in error messages, verbose output, etc.
>>
>> 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:
>>
>> 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.
>>
>> --
>> 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?
>>
>>
> --
> 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 email to std-proposal...@isocpp.org.
> To post to this group, send email to std-pr...@isocpp.org.
> To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/a057117d-4d32-4f39-ac2b-ada996a75ec3%40isocpp.org.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
Even if it's just used for debugging/logging/tracing, it deserves a place. Those are requirements for nearly every program.
>
> --
> 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 email to std-proposal...@isocpp.org.
> To post to this group, send email to std-pr...@isocpp.org.
> To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/01F1ABC6-E4E6-4524-A29A-106FC4FE8470%40googlemail.com.
--
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 email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/878tvfl89r.fsf%40gmail.com.
C# does something similar. The funny thing is that out of bounds enum values print too : https://dotnetfiddle.net/0qwDZkI often do the enum-to-string tricks. I'd be interested if we could really describe what the expected behavior would be. Not sure C# does that right, but not sure what «right» would be in general...
char const* toStringUnchecked(Enum e) noexcept; // #1 UB on invalid value
char const* toStringChecked(Enum e) noexcept(false); // #2 throws std::invalid_argument
std::ostream& operator<<(std::ostream&, Enum e); // #3 writes underlying value if invalid
I've added few examples showing simple implementation of
enumerator-to-string and string-to-enumerator using both the TMP and
constexpr MP libraries to the mirror reflection utilities:
https://github.com/matus-chochlik/mirror/blob/develop/example/mirror/005_enum_to_string.cpp
https://github.com/matus-chochlik/mirror/blob/develop/example/mirror/006_string_to_enum.cpp
https://github.com/matus-chochlik/mirror/blob/develop/example/puddle/005_enum_to_string.cpp
https://github.com/matus-chochlik/mirror/blob/develop/example/puddle/006_string_to_enum.cpp
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e8e400ef-d60f-448c-9594-15c75e9728e9%40isocpp.org.
On Tuesday, 30 August 2016 06:23:04 UTC+1, Matus Chochlik wrote:I've added few examples showing simple implementation of
enumerator-to-string and string-to-enumerator using both the TMP and
constexpr MP libraries to the mirror reflection utilities:
https://github.com/matus-chochlik/mirror/blob/develop/example/mirror/005_enum_to_string.cpp
https://github.com/matus-chochlik/mirror/blob/develop/example/mirror/006_string_to_enum.cpp
https://github.com/matus-chochlik/mirror/blob/develop/example/puddle/005_enum_to_string.cpp
https://github.com/matus-chochlik/mirror/blob/develop/example/puddle/006_string_to_enum.cpp
Is it really necessary to construct the lookup table as a library container? Wouldn't a switch statement perform better?
I searched this list for similar suggestions, but of course I may
have overlooked something.
I frequently use the following pattern:
enum class FooBar { KEKS, DOSE };
char const * const FooBarStr[]{"KEKS", "DOSE"};
I use the strings to in error messages, verbose output, etc.
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:
static_cast<char const *>(FooBar::KEKS) // "KEKS"