Reputation: 1
I am curious to see how the cout function was written, where should i look for it?
My second question is, can i really dissect every component of the language in order to understand it?
For example, can i see how the compiler read the operators? Even if its in machine code, how do i find myself in it?
Upvotes: -2
Views: 205
Reputation: 2420
You can try to analize the assembler code generated from C++ code.
https://godbolt.org/ <-- This is good tool, because it allows to show you how different compilers generate same things. You can compare them.
If you want to see how it is written in C++, you can look at it in iostream
standard library.
Upvotes: 0
Reputation: 28290
A large part of C++ is the Standard Library, which is written in C++. If you want to examine parts of it, there is no problem - just look in the corresponding header file - for cout
, it's iostream
, located somewhere in your file system.
If you're using a debugger (e.g. MS Visual Studio or gdb), you can "step" inside the implementation, as if it was your own code. For example, in Visual Studio, while debugging the following line of code
std::cout << "hello";
you press F11, and it will show you the implementation of the relevant operator<<
.
Parts of the "core" C++ language cannot be examined that way. However, you can examine the generated machine code instead. For example, are you curious how your compiler implements virtual functions? Press Ctrl+F11 in Visual Studio, and it will show you the code.
The boundary between "core" features and "library" features is a bit blurred though. Things like dynamic_cast
may be implemented by special C/C++ functions or machine code - you have to discover it for yourself. Either way, it's easy to debug/examine.
Upvotes: 1
Reputation: 27538
I am curious to see how the cout function was written, where should i look for it?
cout
is not a function. You mean one of the operator<<
member or non-member functions for output streams.
Anyway, even though the C++ standard does not mandate files for standard-library headers, those functions are usually implemented in terms of the language itself and reside in files that come with your compiler. For example, on my Windows system there's a file called C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\include\iostream
, which the Visual C++ compiler uses to process a line like #include <iostream>
in user code.
Expect to see a lot of non-standard, hard-to-read, macro-infested and undocumented internal pseudo-C++ in those files.
My second question is, can i really dissect every component of the language in order to understand it?
Only if the compiler is open source, for example GCC but not Visual C++, and only if you understand enough about compiler writing.
For example, can i see how the compiler read the operators?
Only if the compiler is open source, for example GCC but not Visual C++, and only if you understand enough about compiler writing.
Even if its in machine code, how do i find myself in it ?
Find out how to view the disassembly on your system and with your tools.
Upvotes: 1
Reputation: 6404
Learn an assembly language, probably not x86 as that has become very complicated. Choose a RISC chip such as in the Raspberry PI.
You'll see that languages such as C map quite easily to assembly. Pretty much every C statement has an equivalent which is one or two assembly statements. There are even things like ++ matching to INC (assembly increment) when the language could have been defined as only allowing += 1.
Higher level languages like Javascript don't match as easily. They're designed for ease of programming rather than efficiency. C++ is a difficult one, it does compile to very efficient machine code, but only because the compiler is very sophisticated.
Upvotes: 0