Creator of the site here. Thanks for taking a look and I really appreciate all the feedback.
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.
A suggestion: I would prefer to not have to scroll the code horizontally. C++ tends to be verbose, and so benefits from a wider window/view. Maybe place the intent and description below the sample code?
This is indeed an annoying problem. Please either decrease the font size or use 2 or 4 space indentation (without mixing tabs and spaces) or just steal some space from the sides.
You could always put the type in a tooltip on hover for auto or something like that. That way you get modern code style along with how an IDE would handle it. But yeah, there's no pleasing everyone when it comes to verbosity :)
Big thumbs up for your stance on auto - eminently sensible. There are instances where "meh, you don't need to worry about the type here" are valid, but if your code is to teach or explain then it needs to be as explicit as it can possibly be.
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).
Great looking resource, thanks for putting this together. Is there currently a mechanism to request and vote on samples to add? It would be cool if someone could, for example, request a sample on Expression Templates (or whatever), and other users could vote on which existing requested samples are most interesting.
The current system is just to post sample suggestions to the GitHub issues page [0]. No voting however. I'm not sure there's much need for a voting system right now.
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?
The samples are all very short and generic, so I'm not sure how legal significance a copyright license will have. However, I do plan to add a license to the repository if only to make it clearer.
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?
A single, trivial code snippet may not carry a copyright at all.
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.
Would be useful if it grows and gets a easily search able index.
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.
My biggest concern with Googling for these things is that the Stack Overflow answers range from bad practices, to good C++03 practices, to good C++11/14 practices. It's difficult to know exactly what the state-of-the-art is.
Very true, and the site will be a little less useful for those people. I am also planning to add a note to every sample stating which standard it depends on, which should make this problem a bit better.
Well done. Thinking of it, congratulations for being the first (afaik) to do this, should have been done a long time ago. Looked through quite a lot of the samples and none really raised my eyebrows, which doesn't happen often for the typical 'C++ tutorial' type of site. (not that I'm some kind of authority, I just write C++ almost daily for a living) Especially nice are all the links, also to articles of Meyers etc. More of the latter would be great, Sutter also did a series once which covered a lot of the basics quite well.
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 :]
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.
I like it! Specially because it explains exactly what the code does in a generic way.
But to me, personally, it's far too much "modern" C++ or C++11 oriented, it would be nice to have with the same clean design for the "old" C++.
Any reason for not having any snippet with the "new style" range based for loops ?
My friend sent me this site earlier. As someone who used to code in C++ over 10 years ago and has been desperate to get back into it this site is perfect for me. Thanks for creating this and I hope it expands and grows as I'm already finding it useful :)
No, this book is far too advanced if you're just learning C++. The C++ Programming Language, 4th Edition by Bjarne Stroustrup is the way to go for modern C++ (it covers everything and uses C++11 examples as much as possible). The beginning of "Effective Modern C++" starts off with the subtle distinction between template specialization and auto specialization and arcana for rvalue references vs lvalue references. If you're looking for a light read on C++11 plus, maybe some of the later chapters, but it's a rough start.
"A Tour of C++" by Bjarne is also a nice introduction to modern C++. It's essentially a select few chapters from TCPPPL4 that covers the fundamental bits of the language.
I know you're asking the site creator, but I'd like to explore this matter for their benefit.
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.
While the question was aimed at the repo creator, I did intend on raising the question to incite further discussion since licensing concerns can be frequently forgotten - thank you for your insights!
I need to add a license to the samples repository (license suggestions welcome). The samples are all so short and generic that I'm not sure what legal significance it would actually have, but it's worth doing anyway if only to promote sharing and contributions.
You say "the samples are all so short" but there have been cases [+] where a very few lines were found by a court to infringe a copyright. Keep in mind that the heirs or purchasers of your copyrights might not be as benevolent as you.
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
It has disputed legal effect in the US, too. But people (individuals and businesses) in practice don't seem to be concerned by it -- in either jurisdiction, or most others -- enough to not use software that is dedicated to the public domain like SQLite, so as a creator if your concern is signalling to people that it is okay to use it, a PD dedication seems likely to be clean.
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.)
It would be pretty sweet to have IDE integration with something like this -- when creating a source file in Xcode or Eclipse, have a menu pop up with various curated samples of common C++ idioms.
I will make sure this is made clear in the sample descriptions.
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 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.
[0] https://github.com/sftrabbit/CppSamples-Samples