onurkirmizi
onurkirmizi

Reputation: 41

How are implementations of standard library header files function prototypes are written in c++?

I know that C++ has a standard library which consists of header files which consists of functions prototypes but where are their implementations? I mean I want to see how cout << "hey"; really works. How are implementations of built in functions are written? in assembly?

Upvotes: 3

Views: 97

Answers (1)

Bathsheba
Bathsheba

Reputation: 234615

The burden of implementing the C++ standard library can be discharged in any way so long as the specification is respected. In other words if you #include the requisite header, then you get the function you want.

Much of it is indeed written in C++ and you can view the code with your line-by-line debugger if your compiler toolset ships with the standard library source code. One problem with reading the standard library code is that any variable needs to be one that can't be #defined as a macro by a programmer. Which is why the variable names are prefixed with __ or _ followed by an upper case letter.

Some of this C++ may well have constructs that are non-portable since a C++ standard library implementation is typically tied to the compiler. Indeed some functions (e.g. std::malloc) cannot be written in standard C++!

Some components of the library may well be written in assembler (e.g. std::strlen) or perhaps even hardcoded into the compiler itself.

Upvotes: 4

Related Questions