
A repository of modern C++ code samples curated by the community - edran
http://www.cppsamples.com/
======
sftrabbit
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.

[0] [https://github.com/sftrabbit/CppSamples-
Samples](https://github.com/sftrabbit/CppSamples-Samples)

~~~
usefulcat
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?

~~~
g1236627
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.

------
rectang
What's the licensing of these samples?

Perhaps consider CC0?
[https://creativecommons.org/choose/zero](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.)

~~~
sftrabbit
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?

~~~
detaro
FSF recommends CC0 for small code snippets in documentation:

[https://www.gnu.org/licenses/license-
recommendations.html#do...](https://www.gnu.org/licenses/license-
recommendations.html#documentation)

~~~
rectang
Thanks! Here are a couple more resources on the subject:

[https://creativecommons.org/weblog/entry/27081](https://creativecommons.org/weblog/entry/27081)

[http://labs.creativecommons.org/2011/05/28/fsf-recommends-
cc...](http://labs.creativecommons.org/2011/05/28/fsf-recommends-cc0-for-code-
snippets-in-documentation.html)

------
SCHiM
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.

[0] [http://stackoverflow.com/questions/216823/whats-the-best-
way...](http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-
stdstring) There we go.

~~~
sftrabbit
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.

~~~
a3n
Keep in mind that many shops are explicitly at C++03, or some other less-than-
current state of the art, on purpose.

~~~
sftrabbit
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.

------
ndesaulniers
I think the shared_ptr and unique_ptr examples could better show why you'd
choose one over the other, and what their limitations are.

------
stinos
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 :]

[0] [https://rnestler.github.io/more-rust-compared-
to-c.html](https://rnestler.github.io/more-rust-compared-to-c.html) [1]
[https://news.ycombinator.com/item?id=9329506](https://news.ycombinator.com/item?id=9329506)

------
ausjke
Great site and initiative indeed.

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.

[http://docs.openstack.org/developer/devstack/stack.sh.html](http://docs.openstack.org/developer/devstack/stack.sh.html)

------
AtmaScout
Thanks for this, I hope to see many more examples.

------
gh02t
This is really helpful for me, thanks!

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.

------
lmedinas
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 ?

------
BinaryIdiot
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 :)

Thanks!

------
esaym
Is there a good book "modern C++", kinda like "modern perl" I guess?

~~~
detaro
I've been recommended Scott Meyers' "Effective Modern C++". Haven't gotten to
it yet though.

~~~
ndesaulniers
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.

~~~
bstamour
"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.

------
Calms
Great stuff.

Do you feel explicitly stating any licensing (or lack of) be a good idea for
the samples?

~~~
sftrabbit
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.

~~~
dragonwriter
A public domain dedication, like that of SQLite, is the least restrictive and
simplest option.

~~~
allan_s
but has no legal value in France for example as you can't reject all your
rights.

~~~
im2w1l
Is it possible to declare something public domain, and license it as a
"fallback"?

~~~
detaro
That's the effect of the CC0 license: dedicate the work to the public domain,
if that isn't possible give away as many rights as possible

[https://creativecommons.org/publicdomain/zero/1.0/deed](https://creativecommons.org/publicdomain/zero/1.0/deed)

------
santaclaus
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.

~~~
sliverstorm
We could even give it a paperclip-themed mascot!

 _Hello! I see you 're trying to write a string parser..._

~~~
santaclaus
Oh my god, a Microsoft Bob style IDE would be the most amazing product of the
year...

------
Aqwis
What's the deal with std::experimental? Does the "experimental" part means
they shouldn't be used in production code?

~~~
sftrabbit
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.

------
im2w1l
First: Very nice site, good job!

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.

------
partisan
I like the idea, but the site is unusable on mobile (iPhone 5s).

~~~
sftrabbit
Thanks. I tried testing on as many devices as I could. Could you please submit
a screenshot and description to the website issues page [0]?

[0] [https://github.com/sftrabbit/CppSamples-
Web/issues](https://github.com/sftrabbit/CppSamples-Web/issues)

------
voltagex_
Hoping this expands to more languages. SampleOverflow? It'd clean up the
number of trivial question/answers on SO.

I'd love to see some socket code on the site, at any rate.

------
icpmacdo
Are there websites like this for Java and JS?

