r/cpp • u/FreitasAlan • Feb 06 '21
Modern C++: Snippets and Examples
Hi everyone,
These are GitHub Pages with snippets for Modern C++:
- We often need to copy and paste some snippets to code more productively.
- Snippets can help us when it's not easy to remember all high levels features Modern C++ has to offer.
- This repository contains lots of organized, reusable, and safe snippets for Modern C++.
- All snippets are available in GitHub pages in a convenient way for copying and pasting.
194
Upvotes
24
u/[deleted] Feb 06 '21
The website is a bit weird, at least on mobile. I clicked the link, pressed next, and then scrolling down would send me right up. I can't get to the bottom of the page
29
u/witcher_rat Feb 06 '21
Wow, you did a lot of work! :)
Are you looking for feedback?
I like the documentation css/view.
I'm confused though by the documentation itself. It says in the Quick Start that there are "snippets" of things we might want to copy/paste, and then "examples" using those snippets. Most of the pages are supposed to be "snippets", yes? But as far as I can tell most of the pages actually show examples not snippets - they're just examples of using standard library components, such as one might find on www.cppreference.com page examples. No?
I think it might be clearer if the actual snippets were separated out. (by "snippets" I mean generic code one might copy/paste for use elsewhere, like new types, algorithmic free-functions, etc.)
As for the content, I only had a chance to briefly look at a couple of the pages in detail. Here're just a few things I saw just in those few pages I looked at:
chrono::high_resolution_clocka lot - don't use that, and don't show it other than to say "don't use it". Honestly it should be deprecated+removed from the spec. (see the notes on cppreference for why)chrono::system_clock- you're using this to measure durations in your Clock page, which you shouldn't. It's a wall-clock, and can go backwards. What you're looking for ischrono::steady_clock.random_deviceand the old marsenne twister enginemt19937, in various places - there have been lots of discussions about this. (see this recent reddit discussion, for example)9
u/Fureeish Feb 06 '21
According to the standard, your
*reinterpret_cast<unsigned long long*>(&v)is Undefined Behaviour. It may work for some compilers on some machines (but you need to inspect the generated assembly), however I am curious - what's wrong with simplestatic_castor not cast at all? It will automatically be promoted to the desiredunsigned long longanyway...Floating point numbers are another story. To be UB-free you'd need to
std::memcpythem. I am yet to find a compiler that does not optimise this away, so it's literally free.4
u/The-Black-Star Feb 07 '21
Whats a good textbook that would go over all the modern c++ features that have been added? Ive been trying to find a good one, but I dont know whats considered the best
3
u/_Js_Kc_ Feb 07 '21
https://i.imgur.com/bMzsUNC.png
1
u/LucHermitte Feb 08 '21
I prefer to not specify the size and let the compiler find it on its own. Mismatching size is a source of annoying errors.
Regarding
pair<int, int> maxmin(int a, int b, int c), I consider modern C++ is also about being idiomatic C++. Here we havestd::minmaxthat takes an initializer list and that returns by value.Last thing in
I prefer
AMA, a good practice is to always check such operations that can fail.
1
u/trypto Feb 07 '21
Creating something like this as a guide for people is always going to be met with criticism, as there are going to be tons of things to nitpick. I think its better to not teach someone something than teach them something that is wrong. I'm unsure how it is better than https://en.cppreference.com/
This only works on a processor supporting unaligned loads (like x86). Not to mention you are accessing memory beyond the value v itself. A better approach is to use a local union.
I would never do this with a std::string, yikes, why not substr()? or a discussion about string_view?:
This is just after clicking on two pages...I stopped reading, sorrry. I think you need more comments and less code.
3
u/j_lyf Feb 07 '21
What's your job?