r/cpp • u/kris-jusiak https://github.com/kris-jusiak • Feb 09 '21
[Modern] C++ Tip of The Week
https://github.com/QuantlabFinancial/cpp_tip_of_the_week6
u/evolvingfridge Feb 10 '21
why it starts with 182 ?
3
u/kris-jusiak https://github.com/kris-jusiak Feb 10 '21
The first 181 puzzles haven't been transformed into markdown yet, sorry about that. We are working on it but also wanted to let everyone know that we are committed and already have something to share. More new tips are coming every week and we will add the previous ones as we go to.
2
4
u/AlexAlabuzhev Feb 10 '21
What's the point of mentioning proposals there?
"Tip" is something I can use now, not "maybe in 5 years if the gods are good".
"Proposal of the week" can be an interesting list too though.
3
u/kris-jusiak https://github.com/kris-jusiak Feb 10 '21
There are different types of tips, some of them refer to future proposals, some are related to the performance, some to existing new features, best practises, new techniques, libraries, crazy ideas, etc. We are trying to keep them engaging and as varied as possible.
2
3
u/t4th Feb 10 '21 edited Feb 10 '21
Did C++ committee finally add enum class count() method?
1
u/stilgarpl Feb 10 '21
What would it do?
1
u/t4th Feb 10 '21 edited Feb 10 '21
enum class Color { red, green = 20, blue };Color r;r.count(); // return number of elementsInstead of workarounds like:
enum class Color {red,green = 20,blue,Count // please dont remove};or MagicTemplate stuff.
4
u/stilgarpl Feb 10 '21
enum class Color { red, green = 20, blue };Color r;r.count(); // return number of elements
Instead of workarounds like:
enum class Color {red,green = 20,blue,Count // please dont remove};
or MagicTemplate stuff.
In your first example r.count() will return 3. It's nice to know, but it won't let you iterate over enum values or anything.
Second example is not really a workaround - you have 3 values, but Count will be equal to 22. values 1-19 are invalid.
If anything, it would be better if enum class had .values() that returned collection of its values. That would actually be useful.
2
u/t4th Feb 10 '21
You are right, but the example details are irelevant and your comment is 50% just nitpicking.
The issue is that this is useful feature with simple solution, that hasn't been added for years. It is like another include guard case.
0
Feb 10 '21
[deleted]
7
u/kris-jusiak https://github.com/kris-jusiak Feb 10 '21 edited Feb 10 '21
Yeah, sorry about that, not all tips have been fully transformed yet but we are working on it, around 180 old ones still to go...
8
u/lenkite1 Feb 10 '21
I really wish some in-depth explanations were also there with these tips which just show sample code and a statement.