Reading code is one of the best ways to learn new idioms, tricks, and techniques.
Sadly it's very common to find badly written C++ code. Some use C++ as if it was C, others as if it was Java, some just shoot in their feet.
I believe
gtkmm
[1] is a good example of C++ design, but a binding could not be the best code to read (you need to know the C library behind it).
Boost libraries (at least the one I read) tend to be less readable than I'd like.
Can you mention open source projects (or other projects which source is freely readable) that are good example of readable, modern, well-documented, and auto-contained, C++ code to learn from?
(I believe that one project per answer will be better, and I'd include the motivation that led you to selecting that one.)
EDIT: The question was not intended from a newbie point of view (even if good readings for newbies are welcome, I don't believe that it should make any difference). The reading issue with boost is primarly, as noted in comments, that it's filled with compiler dependent workarounds that make its prose less fluent.
EDIT2: It was not my intention to dismiss all the boost libraries. It's just that trying to read, as an example, the BGL I didn't found the prose I was searching for (even if you can eventually understand them). If you have better examples from boost just point to the specific library.
In my humble opinion I think dlib [1] has some of the most readable code around. Modern style, RAII, no pointers, religious use of contract programming, etc.
But I maybe biased as I run this particular open source project :)
[1] http://dlib.netI see that you've already decided that Boost is "less readable than [you'd] like", but Boost is quite possibly the best example of exactly the kind of source code you're looking for. The unfortunate fact is that in spite of all of its positive characteristics, C++ itself is less readable than you'd like.
The more time you spend reading Boost code, the more C++ you'll understand and the more readable it all becomes.
At the op's suggestion, here are some libraries I think are pretty good:
There are many more to choose from.
One of the things that makes Boost good, and still readable, is that most of its libraries consistently factor out the platform/compiler-specific tweaks and leave the primary functionality as portable as possible. It's a really, really great thing for C++ devs to learn.
Note: when I say "portable" or "not portable" above, I'm not talking about the library as a whole, but rather the underlying implementation that a reader would be looking at. In other words, "not portable" means lots of implementation details that are particular to a platform. Boost libraries are amazingly portable in practice; I don't want to be misunderstood.
You could have a look at:
Some projects NOT to learn from:
Also I wouldn't recommend reading the Boost code if you are a C++ newbie, you will get buried in little details.
[1] http://dlib.net/std::string
-- a mark of a codebase developed before std::string
was considered reliable by many developers. +1 - Billy ONeal
I've played around some with Google Test [1] (Google's C++ unit testing framework) and have been impressed with its clean design and clarity of code. It's also interesting to me how Google Test's adherence to the Google C++ Style Guide [2] enhances its readability (even though I don't always agree with the Google C++ Style Guide).
I would expect other notable C++ projects from Google, such as Chromium [3] (the codebase for Google Chrome) and Protocol Buffers [4], to be similarly high quality.
[1] http://code.google.com/p/googletest/Another well-written C++ project is the Monotone version control system [1].
[1] http://www.monotone.ca/I would say: if the code is beautiful, easy to read, modern, portable and uses latest goodies of C++, then probably you are reading textbook...
Unfortunately most of modern code is not so easy to read and far from being beautiful... Because real life is quite hard.
Examples of greatest modern software libraries that extremely useful and good designed Boost and C++.
dynamic_cast
or other stuff. But it is great, well debugged high quality software.Bottom line, programming is hard. Take a look on this code [1] I had written. It is code of cache system with invalidation, triggers, LRU and timeout support, that can be shared between forked processes and normal threads... But it is damn hard to read and understand. But is very valuable piece of software to me that required lots of writing.
(And it is uses STL, smart pointer, allocators, RAII and many other goodies)
P.S.: I do not recommend to learn from my code ;-)
http://www.virtualbox.org - mostly because it's a complete,complex and large application(s) - and not "just" a library.
I'd learned a lot from the source code of http://code.google.com/p/protobuf/ as well.
I think one of the projects that i've encountered that definitely deserve praise by the cleanliness of the design and source is LLVM [1]. This library is both very complex, but very modularized, so you probably won't need to learn much about the library that is tangential or unrelated to what you want to achieve. The interesting thing about llvm is that they don't believe that there is a tradeoff between good design and good performance targeting. For example, there are quite a few scenario focused containers implementations [2] that provide optimizations in specific circumstances.
The source and documentation is a pleasure to read, and they take regression testing very seriously.
If someone would like to learn how to design a complex c++ codebase that is mantainable and well designed, is encouraged to study LLVM
[1] http://llvm.org/I think ACE [1] is a good example of a mature open-source multi-platform C++ library with a lot of available documentation [2]. A lot of the documentation is in the form of academic papers, so you learn a lot about the underlying algorithms, design patterns, and tradeoffs.
However, note that it originated some time ago, and suffers from some ugliness in order to be compatible with all the C++ compilers it has ever supported. So it may not fit your desire for "modern" C++ code.
[1] http://www.cs.wustl.edu/~schmidt/ACE.htmlI've only been out of school for about half a year so this may not mean much, but the Hotspot JVM is the best documented program I have ever worked with (of any language). This includes almost all of the toy programs from university courses with the exception of the OS161 operating system.
Speaking of which, that's another example of great, (very)well-documented C++ code. But it's all C, so it's far from modern.
Hotspot has good documentation throughout, and at every level of abstraction, which is rare. Most of the time programs settle for good commenting, which explains the local details of how code works, but no more. With hotspot, there are plenty of resources describing how the whole thing works, most files have comments saying what goes on in there. So do classes and methods. When certain things are done for optimization purposes, this is noted.
How about OpenSceneGraph? A great rendering engine with very clean structure.
delete
inshared_ptr
). The problem with reading code is that it will take time until you really understand the importance of details. - David RodrÃguez - dribeas