3 ms·
This is terrible. You can't just do Enum::Member.str or something?
by fluoridation 2y ago
This is terrible. You can't just do Enum::Member.str or something?
- Aardwolf 2y agoOuch, I first thought this was the example of how to do these things _before_ reflection is introduced to C++ The for loop required to do enum to string really makes it
- einpoklum 2y agoYou can use `.name()` and that also works fine. Remember std::string is a heap-based thing.
- kllrnohj 2y agoThat'll inevitably be a utility function that exists, but C++ generally prefers broadly useful language primitives over single-case helpers
- j16sdiz 2y agoThis is how C++ language usually works. Just some primitive for building libraries on. Expect some library change come later.
- coopierez 2y agoThankfully it is so easy to quickly import libraries into C++...
- pjmlp 2y agoIt actually is, for anyone using Conan or vcpkg.
- coopierez 2y agoSo our team switched to vcpkg recently and, while it has improved certain parts of our dependency process, it has also made other parts more complex. Notably when something suddenly goes wrong it is far more complex to figure out what actually happened (Though to be fair a lot of these issues also come from combining vcpkg with cmake). This led to most of my team revolting against vcpkg and now it looks like we might go back to just vendoring our libraries again. I suppose I just yearn for an all-in-one build system + package manager like exists in Rust or Go. Once you've seen what can be possible when these things are integrated from the ground up it sort of ruins your C++ build experience!
- pjmlp 2y agoUntil one needs to step out of a pure Go or pure Rust experience, and then it is a quite interesting build.rs file, Makefiles or shell scripts.
- fluoridation 2y agoThis would be the standard library so it's not really an issue.
- andersa 2y agoOf course not. We must involve 10 layers of templates that mere mortals cannot read and compilers cannot process in reasonable time so the academics at the committee will be happy. Addressing real problems with simple solutions isn't allowed.
- meindnoch 2y agoWhat are you talking about? At runtime, an enum value is just an integer. You need to look up the enum case that corresponds to your integer in order to convert it to a string. Which is precisely what the code above is doing.
- kamray23 2y agoYeah, C++ enums are just numbers that are really good at pretending to be types. And for that reason they're not actually objects that contain things like their name. And they probably shouldn't, in the vast majority of cases it would be an utter waste of space to store the names of enum members along with them. So you have compile-time reflection for those instead. And yeah, you could implement some kind of thing pretending to be a member but actually being a reflection thing but that's both horrifying and limited so C++ takes the reasonable approach of just adding general reflection in instead.
- fluoridation 2y agoSure, but reflection requires some support from the compiler either way. There's no reason why if you have an expression like x.str, where the compiler can see that x is an enum, that it can't rewrite it into a table lookup like __Enum_strings[x]. This would work even if x is an unknown run-time value. This is basically what any other language that supports converting enums to strings natively does. I understand that the C++ committee prefers to delegate work to the standard library, but in this case it's stupid. Reflection needs support from the compiler. Just add new syntax and put it on the compiler, ffs!
- meindnoch 2y agoSo your gripe is that you have to write `enum_to_string(x)` instead of `x.str()`? And this is of such importance, that this needs to be included in the C++ language itself, as a special case of the dot operator. Correct? >Reflection needs support from the compiler. Just add new syntax and put it on the compiler, ffs! Converting enums to strings is one use case for reflection. Do you suggest introducing bespoke syntax for the other use cases too?