r/cpp • https://github.com/kris-jusiak • Feb 09 '21

[Modern] C++ Tip of The Week

https://github.com/QuantlabFinancial/cpp_tip_of_the_week
106 Upvotes

16 comments sorted by

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.

2

u/kris-jusiak https://github.com/kris-jusiak Feb 10 '21

Agree, that would be nice, something to improve on as we go. Thanks, for feedback. The current idea is to have a quick explanation, link to the spec followed by a simple code example and a puzzle with failing tests to solve.

6

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

u/kevkevverson Feb 10 '21

Was first used on the Blink engine

1

u/specialpatrol Feb 10 '21

^ sheer wit, right here.

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

u/GYN-k4H-Q3z-75B Feb 10 '21

Some of these are pretty interesting. Will keep track of the list.

2

u/kris-jusiak https://github.com/kris-jusiak Feb 10 '21

Thanks, appreciate it :)

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 elements

Instead 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

u/[deleted] 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...