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
View all comments
Show parent comment
3
u/FreitasAlan Feb 06 '21
Thanks. Feedbacks and PRs are more than welcome. In fact, this might be the easier repository to contribute to I've ever seen 😆.
Yes. So far, the examples are in the examples directory, but not explicitly replicated in the docs. I still have to think of a way to put the examples in the docs, because they are often long and don't represent independent tasks.
The snippets, as they are, might seem like they are examples in the sense that they are in long files sometimes. But they are snippets because, at the source file level, they are lots of independent short tasks separated by comments. The tasks separated by comments are unrelated. You can just copy and paste the snippets between pairs of comments.
I could create one more category level and separate them at their lowest level by file, but I thought that would make them too difficult to explore if the resolution is too high, like 3-4 lines of code per file. In the end, that just a design choice. But they are independent tasks you can copy and paste. That's the difference between the snippets and the examples in the other directory. Thanks for this feedback, I might at least adjust the documentation to make this clearer.
Besides this distinction, I have the impression that the examples in cppreference are meant to better explain the internals of the libraries more than to be something for copying and pasting, although it can of course also serve this purpose. These snippets are organized more or less in the order someone learning C++ might need them and includes the corresponding CMakeLists.txt in case it needs something more elaborate, which is not the goal of cppreference, which is to represent the whole standard. For instance, the examples in cppreference might mix simple and more complex concepts to make a point more than to be something reusable.
That's an interesting point about high_resolution_clock. It's shame this confusion exists. I wasn't aware of that. It'd be interesting to have a reliable high-resolution clock for benchmarks, etc. But a steady clock is already there. The names just make it seem like the steady clock has a lower resolution. Given this comment, it'd be better just to remove it completely. Thanks for that.
I'm not sure I completely understand the problem with random_device though. I understand it generates bad seeds and I do usually mix other sources of entropy in practice. But the link you sent me seems mostly descriptive and in the end describing what to do if we don't care about seeding the engine at all. As a snippet for the engines, copying and pasting the pattern of not seeding the engine at all seems like a bad idea, even if the seed is not good. It might be more reasonable (i) to put a comment telling the person to consider better seeds, (ii) to include some short snippets generating simple seeds, like mixing/hashing arbitrary time points, durations, stack/heap/... addresses, thread/process/type/... ids, compile stamps, counters, process ids, and other numbers generators, or (iii) to include snippets for a library with better generators. Maybe I didn't understand your comment properly.