6

I'm curious about learning how certain C++ features work. I'm trying to learn C++11 concepts such as std::function, but I keep hitting walls like INVOKE(function, arguments, return) that I don't understand. People tell me, "Oh, just ignore it and use auto" but I want to have a truly deep understanding of how C++ and its standard library works, so I wanted to find the source code of the standard library.

I would guess that the C++ Standard Library is somewhat related with the C Standard Library and the messy assembly/binary implementations at the lowest level for stuff like std::iostream and such, but I'm interested in more higher-level abstractions like smart pointers and std::function. Given that many of the C++11 libraries were once Boost ones, how might I find the source for C++ Standard Library implementations?

Mat
  • 202,337
  • 40
  • 393
  • 406
CinchBlue
  • 6,046
  • 1
  • 27
  • 58
  • 2
    Locating the sourcecode is one thing that's almost trivial, but understanding it can be a challenge. The problem is that the code needs to require very strict rules and and be performant at the same time, so you have lots of optimizations at the price of readability. Concerning `std::function`, consider writing one yourself, including the code for `std::bind`. This will give you valuable insights into the issue. – Ulrich Eckhardt Mar 16 '15 at 06:44
  • 1
    c++ stl is implemented mostly on headers so a very good start would be jump to definition in your IDE or something like cscope – bolov Mar 16 '15 at 06:44
  • 1
    The STL is not the C++ Standard Library and in particular `std::function` is not part of the STL, @bolov. – Ulrich Eckhardt Mar 16 '15 at 06:45
  • If you have an IDE, at place ,such as#include header click mouse find declare or implement – Ron Tang Mar 16 '15 at 06:46
  • The source code for glibc is freely available, and I once looked at it for the implementation of printf(). All I can say is, well, yeeeuurrkk! That's not fair, glibc is a good library as far as I'm concerned, but it is necessarily complicated. But perhaps the best thing to do is to download the source code from gcc.gnu.org, and point Eclipse at the unzipped source code for the library and let Eclipse index it. That way you can start finding out about the structure of the code, so long as Eclipse's indexer is up to to the job. – bazza Mar 16 '15 at 06:50
  • 1
    @UlrichEckhardt I know, I was just too lazy to type in C++ Standard Library and didn't care enough how I called it, as long as the point of the message was clear. But you are right. And damn you for making me type this much :) – bolov Mar 16 '15 at 07:04
  • Check out [Is there a readable implementation of the STL?](http://stackoverflow.com/questions/2127612/is-there-a-readable-implementation-of-the-stl) (I would also recommend [ustl](https://github.com/msharov/ustl) ) – nimrodm Mar 16 '15 at 07:07

2 Answers2

15

The two most popular open source implementations of standard C++ library are:

Both websites contain links to git/svn repositories with source code.

  • I have currently downloaded and pulled from the git read-only repo for GCC and am currently reading libstdc++! It looks pretty arcane but I suppose I am happy. Thanks all! – CinchBlue Mar 16 '15 at 09:47
  • 1
    el.pescado could you also add MSVC STL here? https://github.com/microsoft/STL – pratikpc Feb 28 '21 at 06:08
3

You might dive into the source code of libstdc++ if you care about GCC. Indeed it sometimes leverages above the standard C library (e.g. ::operator new might call malloc, etc...)

Notice that since the C++ library is part of the standard, some of it might be implemented in a compiler specific way.

In principle, nothing requires standard headers to be real operating-system files; a compiler could parse #include <vector> in a particular way not involving any file. I know no compiler doing that much!

In particular, libstdc++ is using some GCC builtins and some GCC attributes (which happens to be also understood by Clang/LLVM).

And some standard types require (or profit from) internal support in the compiler. For example, GCC has some specific code to deal with va_list and with std::initializer_list (and of course basic types like int...), etc. Also, the compiler's implementation of C++ closures (or lambda functions) is related to other classes, etc...

Also, some optimization passes of GCC (it has several hundreds of them) are designed with some features of libstdc++ implementation in mind.

BTW, using a recent gdb debugger with e.g. the libstdc++6-4.9-dbg debian package might also help.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • _"Also, the compiler's implementation of C++ closures (or lambda functions) has to be compatible with its std::function implementation, etc..."_, nope, a closure is just a class type with `operator()` so `std::function` doesn't need to do anything special to support it. – Jonathan Wakely Mar 16 '15 at 10:51