Timeline for Rock, Paper, Scissors in C++
Current License: CC BY-SA 4.0
7 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 10, 2020 at 13:24 | history | edited | CommunityBot |
Commonmark migration
|
|
Dec 9, 2019 at 11:18 | comment | added | Laurent LA RIZZA |
To sum up, I think the solution with enums is fine, but you have to make concessions, until enum s have full power : using enum for C++20, compile-time reflection for : the number of enumerators, the list of enumerators, the string_view s on the enumerator names, and the automatic generation of to_string .
|
|
Dec 9, 2019 at 11:09 | comment | added | Laurent LA RIZZA |
@Edward : This is one of the yucky points of enum s (scoped or unscoped). You can't (yet) introspect the number of declared enumerators, nor generate a dumb string from the enumerator name. There is a common technique of declaring an extra enumerator NB_ENUMS at the end (your enum had better be scoped, or you'll get name clashes), and you don't use it in user code, only to declare appropriately-sized arrays and such. This limits the power of enum s. There is no satisfactory solution yet, until we get proper compile-time reflection in C++. The C preprocessor might help there :)
|
|
Dec 9, 2019 at 11:02 | comment | added | Edward |
I meant the part where a Choice is turned into text for output. I’m starting to think that perhaps a non-enum class might be a nicer option. What do you think?
|
|
Dec 9, 2019 at 10:54 | comment | added | Laurent LA RIZZA |
@Edward : you've got an example here : your choices array is properly tucked away in the getBotChoice function. The static_cast (or C style cast) would be present only once, not scattered at every call. enum s or enum class es all the same will have drawbacks until C++ gets proper compile-time reflection on them.
|
|
Dec 9, 2019 at 10:35 | comment | added | Edward |
While I also often suggest using enum class , in this case it seems to me that it’s better as a plain enum for brevity and clarity. Otherwise one would have to use a static_cast to use it as an array index.
|
|
Dec 9, 2019 at 9:12 | history | answered | Laurent LA RIZZA | CC BY-SA 4.0 |