Comments (2)
Hi :)
These syntaxes do look nice, but the tricky part is how to get them parsed and output something that will get parsed by the compiler, and how to keep them optional.
Currently, Better Enums takes advantage of being able to dump the ...
inside BETTER_ENUM(Foo, int, ...)
directly into an enum class
, so enum class {...}
, and, parsing only top-level commas using the preprocessor, into an array declaration in slightly modified form, so _values = [slightly_modified(...)]
. From there, the compiler takes over the parsing. You can see a simplified, but fully-working sketch in this StackOverflow answer. It focuses exactly on what Better Enums does to get the compiler to parse the declarations.
Having these inner curly braces seems like it will prevent this parsing. We could probably get them treated as initializer lists for the _values
array declaration, for some type that casts to integers, but I don't think they would parse at all inside an enum class
. In fact, being valid syntax for enum class
is going to be the limiting factor in the current Better Enums approach, I think.
I had some ideas for using some overloaded operators to associate metadata with constants. But, the only operator that can appear to the right of an enum constant inside enum class
is =
, and it's not actually an operator, but another piece of syntax, in that context. So, basically, I believe we can only use =
. The only way we can get to use other operators inside an enum class
is by asking the user to always use =
, then on the right side we get an expression, and we can ask the user to write just about anything in there. But then we lose automatic value assignment... So, I'm not sure what to do in this approach.
There may be better ways of generating the enum class
than just pasting in a big, opaque token string like done now. I haven't really kept up with C++17, so perhaps there is something there :)
I recall messing around with heavier preprocessor parsing, like done by Boost, but I didn't find the results satisfactory, and I don't seem to have documented why. You're welcome to experiment with it, and I'd be happy to know the results :)
from better-enums.
There is also this, about building bidirectional maps between a Better Enum and any types. Together with switch
exhaustiveness checking, it should give you fully-checked compile-time maps. Not sure how good it is in practice, though.
from better-enums.
Related Issues (20)
- Static functions for enum types HOT 1
- Use in a packed struct HOT 1
- MSVC /permissive- leads to "expression did not evaluate to a constant" HOT 3
- Compiling example code: 5-map.cc fail HOT 2
- enums stops being enums HOT 1
- What is the tool to run better-enums in REPL mode in README.md? HOT 2
- Compile-time name trimming and binary size
- How to reflect to enum with type and value strings? HOT 4
- Need to have standard Debian packaging capability
- Cannot use better_enum from class HOT 1
- Using Better Enums in std::unordered_map as key leads to 'message : see reference to class template instantiation'
- Timeframe for new release / tag
- Provide an update to make_macros.py script to use python3
- Set up GitHub Actions
- C++17 interop: std::optional HOT 2
- Way to opt out of string conversion? HOT 2
- cannot add better_enum values to unordered_set HOT 3
- Member '_value' was not initialized in this constructor HOT 1
- Better-enum in maps HOT 7
- Are there guarantees on size with better enums? HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from better-enums.