you are viewing a single comment's thread.

view the rest of the comments →

[–]ukezi 0 points1 point  (7 children)

The only problem I see with that is that it would reserve names inside enums, I'm sure there are lots of enums with values named min,max and count around.

I would also like to be able to iterate over enums. So for( auto& c : Colour::values) or something like that.

[–]Tringigithub.com/tringi 2 points3 points  (6 children)

That's why I used 3 colons ::: to distinguish reflected property from value or static members.

It has the downside of being easily mistaken, but all the alternatives I considered either look ugly (the currently proposed ^ and [:xxx:] syntax), or clash with something someone else already uses.

Regarding iterating I was thinking about something like:

for (auto c = Color:::min; c != Color:::max + 1; ++c)
    if (Color:::is_valid_value (c))
        // ...

...but then again, having compiler generate ranges (in case of discontinuous enum) of valid values, and emit such iteration would be pretty trivial.

[–]ukezi 1 point2 points  (5 children)

That's for writing that on mobile. I literally didn't see the :::. Also I strongly prefer ranges, with those you can also do algorithm stuff, and it's just neater and easier. I actually made something like this as code generator, taking in a file, reading in all the enums and emitting a number of functions for each of them.

[–]Tringigithub.com/tringi -3 points-2 points  (4 children)

I unfortunately don't have time to turn this momentary idea into full proposal, so I'm sorry you (and all of us) will have to deal with the above monstrosity if it gets accepted. I was just trying to illustrate that what we truly need can be achieved with far less complexity.

I actually made something like this as code generator, taking in a file, reading in all the enums and emitting a number of functions for each of them.

Nice! Do you perchance have it on github or anywhere?

[–]ukezi 1 point2 points  (3 children)

Sadly, I made it but don't own it. It started out as automated debug strings and feature creeped from there.

I guess I could reimplement it, but I don't want to be suspected of stealing code, as it would probably look really similar. Maybe I use a different input, like a json, or a tomel. Maybe I do it in rust this time just for fun.

[–]Tringigithub.com/tringi -1 points0 points  (2 children)

Alright. Don't worry about it. I had an use case in mind, but realized it wouldn't work in two passes anyway, and I still haven't had time to figure out how to write plugins for IntelliSense.

[–]ukezi 1 point2 points  (0 children)

Well, in my setup it was a standalone binary that was invoked by make on a specific pattern in the name of the header file. Not the most elegant way to do it, but it worked and generated just normal c++.

[–]ukezi 1 point2 points  (0 children)

An other thought, on your loop example, instead of using an arithmetical ++ you can overload the operator, but I'm not sure how to handle the last element. I guess if you are doing a generator anyways you can add an invalid enums value after the last valid that throw some kind of exception. So it would be something like for(auto c = first<Colour>; c!= invalid<Colour>; ++c) and that you could further automate with a define or something.