A repository of modern C++ code samples curated by the community
cppsamples.com
cppsamples.com
I wanted to provide a place for beginners to learn about modern features of C++ and a quick reference for idioms and best practices. As the disclaimer says, the existing samples all need review, so don't take anything as gospel. If you would like to contribute please check the samples GitHub repo [0] or leave comments on the sample pages themselves.
A few good points that keep coming up:
- The Rule of Three sample especially needs a rework (it will become the Rule of Five later today).
- It isn't supposed to demonstrate the best C++ formatting style, simply because that would come down to personal preference.
- I have opted to not use `auto` unless necessary for the sake of being explicit about types so that beginners have something to reference against. This is important regardless of ones particular preference for when `auto` should be used and it would be impossible to please everybody. I will be adding a note to the site soon.
- I plan to add a "requirements" section to the sample information which will state which version of C++ the sample depends on.
Edit: that's not to say that there aren't also cases where you should use auto because it is safer than using an explicit type (some integer sizing/signing scenarios spring to mind).
Also, please do s/c++/C++/g, especially in the header.
Perhaps consider CC0? https://creativecommons.org/choose/zero I think CC0 would allow them to be reused in any project no matter the license, without requiring attribution. Do others agree with that?
Regardless, best practice for a project which uses any of these samples would be to record the origin in version control, even if credit is not given in the project source code.
It's really important that sample code have clean provenance, since others will be inserting it into their own projects then possibly mutating it and making it difficult to extract. Are these samples all completely original? And will there be any IP controls as far as what you accept for Github pull requests?
(edit: don't try to use markdown for links.)
Is CC0 appropriate for code? I was thinking of going with MIT, but that would require retaining the license in any derivative works. Anybody have thoughts on this matter?
https://www.gnu.org/licenses/license-recommendations.html#do...
https://creativecommons.org/weblog/entry/27081
http://labs.creativecommons.org/2011/05/28/fsf-recommends-cc...
But a whole collection of such snippets will fall under copyright, even if each individual bit alone wouldn't qualify.
So it is important to make the licensing explicit here. CC0 / Public Domain is a natural choice, given that the code snippets are meant to be used individually and that no "real" license would make any sense on individual code snippets.
I can't count the number of times I couldn't be bothered to write boiler-plate C++ to eg. strip a string [0].
I know that if I google the question I'll get a stackoverflow question with the answer. A dedicated site would be an improvement in my opinion. Mostly because of over-zealous mods on SO that confuse the reality of SO with their personal vision of what is good for SO.
[0] http://stackoverflow.com/questions/216823/whats-the-best-way... There we go.
I hope this will also help with cases like: earlier on HN there was the C++ vs Rust article [0] and corresponding comment thread where one valid complaint was the author was comparing 'C++ which no-one writes in production' vs Rust or something in that spirit [1]. It would be great if this site becomes an authorative source one can point to, instead of the huge amount of scattered resources there is now. Like now you have cppreference for the dry theory, cppsamples could become that for putting it in practice :]
[0] https://rnestler.github.io/more-rust-compared-to-c.html [1] https://news.ycombinator.com/item?id=9329506
However can you make the indent to 2 or 3 char? 8 is too luxury in a web-browser these days.
Openstack has a different way to show comments and the code in parallel, the color does not match the text somehow, but it's another way to do it.
I only use C++ in bursts, so while I'm at least competent enough with the basics, I usually forget how to do specific things and have to google the same few things over and over again. It looks like you already have several of the things I'm prone to forgetting, so this is really perfect! Looking forward to seeing more.
Any reason for not having any snippet with the "new style" range based for loops ?
Thanks!
Do you feel explicitly stating any licensing (or lack of) be a good idea for the samples?
As for suggestions, it depends entirely on your motives. The GPL would force users of your code to license their code under GPL also (if distributed to others.) An MIT or BSD license would only require attribution (a copyright notice) in the distribution. Personally? I'd prefer you went MIT or BSD.
+ - I've worked with former employees of companies where this kind of thing happened. I know this is vague and anecdotal, but I'll see what public information I can find just in case someone Really Must Know these cases
And any license text is unlikely to have been vetted by lawyers familiar with the legal system of every foreign jurisdiction where it might be applied, so you probably have the same problem with them (and the more complex the terms are, the more likely that they have some unforeseen problem in an unconsidered jurisdiction.)
People reading the page also will be "not sure", and that is a problem for them, because they need to be sure they can use the code.
The code is hosted on GitHub which encourages authors to choose an open source license. I say "encourages" and not "requires" intentionally. See [1] for GitHub's take on it.
As an employee of a commercial software company, I'm willing to take others' advice with regard to best practices, security implications, code efficiency because I can't possibly be an expert in everything. Generally, I'll prefer someone else's solution over my own if it's well designed, documented, and simple. I'd love for the best solutions to each problem to propagate into everyone's code. But I must know the status of my ability to use your code - I must have a license. If there's no license, I simply can't use it. If there's a license, I can at least consider the code, and I can consider whether and how it affects the rest of my company's codebase.
1 - https://help.github.com/articles/open-source-licensing/#what...
Hello! I see you're trying to write a string parser...
Experimental means that it's part of Technical Specification - basically the proving ground before being accepted into the C++ standard. It is truly cutting edge stuff, so should only be used where there would not be drastic consequences if it were to suddenly change or disappear.
I think you should remove the bubble sort snippet. A newbie may use it verbatim without realizing there are better algorithms.
It may be good to give an example of RAII that protects a resource other than memory, to plant the idea of wider applicability in your reader's head.
I'd love to see some socket code on the site, at any rate.