0

Where can I find the source code of a library file?. I am new to C++ , and I am very much eager to see the implementation file for a library file?

#include<iostream>
#include<list>
#include<iterator>
using namespace std;

int main(void)
{
    list <int> a;
    list<int> :: iterator it;
    a.push_back(2);
    a.push_front(1);
    a.push_back(3);
    for(it=a.begin();it!=a.end();it++)
    {
        cout<<*it;
    } 
    //prints 123
    return 0;
}

In this code, I want to know the implementation of push_back() and push_front(). Where can I find it?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Check the header files and/or if you have VC++, check C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src (change directory for whatever version of VC++ if you have) – Tumbleweed53 Dec 03 '20 at 18:36
  • 3
    If you're trying to learn how to _use_ these function from how they are _written_, stop. Look at [documentation](https://en.cppreference.com/w/cpp/container/list) and consult your [C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if you want to know how to use these. Libraries are written in incomprehensible ways and you will only learn bad habits. Just looking for curiosity's sake is fine, but isn't useful. – alter_igel Dec 03 '20 at 18:37
  • It depends on what implementation of the C++ Standard library you use. The source code is available for [libstdc++ (GNU)](https://github.com/gcc-mirror/gcc/tree/master/libstdc%2B%2B-v3), [libc++ (LLVM)](https://github.com/llvm/llvm-project/tree/master/libcxx), and [Microsoft STL](https://github.com/microsoft/STL), which are the mainstream implementations. – Daniel Langr Dec 03 '20 at 18:37
  • Much of the standard C++ library is templated, and as such is implemented only in header files (the rest is at the discretion of the library author), so you can usually just look in the standard library's header files, in this case in ``. – Remy Lebeau Dec 03 '20 at 18:40
  • 1
    @DanielLangr source code is *always* available for template classes like `list`. – Mark Ransom Dec 03 '20 at 18:41
  • 1
    Related: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/) – Remy Lebeau Dec 03 '20 at 18:42
  • 4
    ***I am very much eager to see the implementation file for a library file?*** For someone new to the language looking at the source will be extremely difficult. It's not the most readable code. – drescherjm Dec 03 '20 at 18:42
  • https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_list.h#L1210 – Evg Dec 03 '20 at 18:44
  • https://github.com/microsoft/STL/blob/master/stl/inc/vector#L707 – rustyx Dec 03 '20 at 18:46
  • 3
    Important note about reading implementation code like the Standard Library implementation that comes with your compiler: It's going to cheat like hell. The library is likely to be tuned for a specific compiler and maybe even a specific hardware architecture. The writers will know exactly how some [undefined behaviour](https://en.cppreference.com/w/cpp/language/ub)s will be interpreted by their compiler for the target hardware and they will take advantage, performing acts that could get you into real trouble if you attempted it without the specialized knowledge they have. – user4581301 Dec 03 '20 at 18:52
  • Standard Library implementations can be worth reading so you know how to better use them for special cases where that last few percent of efficiency is needed, but those benefits will not port and might not even port to the next update of the same compiler. But to know what you can get away with and where, you already have to have a high level of understanding. This is not something you can use as a learning resource until you already know C++ very well. – user4581301 Dec 03 '20 at 18:54
  • @MarkRansom I meant available online. Should have emphasized this. – Daniel Langr Dec 03 '20 at 18:55
  • The std library itself can do many things that are - according to the current state of specification - undefined behavior. They are only valid because the library vendors and compiler vendors agreed that it is valid there. There is e.g. a proposal to change the specification so that [dynamic construction of arrays](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0593r6.html#dynamic-construction-of-arrays) won't be technically undefined behavior anymore. So you have to be careful because you might see things that are technically undefined behavior. – t.niese Dec 03 '20 at 19:06

1 Answers1

2

Where can I find the source code of a library file?. I am new to C++ , and I am very much eager to see the implementation file for a library file?

That depends entirely on the library. There are open source implementations of the standard libraries, so you can do a search for something like iostream source code. You might also want to check with the supplier of whatever compiler and development system you're using. But some libraries, e.g. those that you license from a vendor, may not make their source code available at all.

Caleb
  • 124,013
  • 19
  • 183
  • 272