Standard Library Header

112 views
Skip to first unread message

sachin...@gmail.com

unread,
Jan 12, 2018, 11:52:24 AM1/12/18
to ISO C++ Standard - Discussion
Hello,
In chapter 9 of C++ programming book by Bjarne Stroustrup, I have come across a statement
"An implementation is allowed to take advantage of knwoledge o the standard library definition to optimize the standard library implementation and the way standard headers are handled. For example, an implementation might have knowledge of the standard math library built in and treat #include <cmath> as a switch that makes the standard math functions available without reading any file"

I couldn't understand how <cmath> can be used as switch and standard math functions be made available without reading cmath header?


Regards,
Sachin

Sarfaraz Nawaz

unread,
Jan 12, 2018, 12:22:56 PM1/12/18
to std-dis...@isocpp.org

Well,  What Bjarne meant (in my opinion) is that when the compiler (or rather the preprocessor) encounters the line `#include <cmath>` in a .hpp or .cpp file, it may just use it as an indication that the header/cpp file is going to use the math library and thus the compiler allows it to use the built-in mathematical function (without even reading any header file at all). Bjarne is referring to such an indication as switch (a compiler-switch).. and there does not have to exist any file with name `cmath` and the compiler, thus, does not have to read any file at all.


--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.

sachin...@gmail.com

unread,
Jan 14, 2018, 7:09:02 AM1/14/18
to ISO C++ Standard - Discussion
Thanks Nawaz. Now I understand this. So you are saying we can explicitly write the declarations of standard headers based on the switch instead of depending on the cmath file.
I have one more query from the same chapter, section "Linkage to Non-C++ Code". Here auther says, "Cooperation can be difficult between program fragments written in different languages and even between fragments written in same language but compiled with different compilers". For example, different implementations of the same language may differ in their use of machine registers to hold arguments, the layout of the arguments put on stack....

and the solution auother provide is use of extern "C" 

Here I don't understand how extern "C" is going to solve the layout of the argument put on stack... e.g. On Windows, the argument of a function are put on stack from high address to low address (little endian), and on Unix the argument of a function are put from low to high address (big endian)

Regards,
Sachin


On Friday, January 12, 2018 at 9:22:56 AM UTC-8, Nawaz wrote:

Well,  What Bjarne meant (in my opinion) is that when the compiler (or rather the preprocessor) encounters the line `#include <cmath>` in a .hpp or .cpp file, it may just use it as an indication that the header/cpp file is going to use the math library and thus the compiler allows it to use the built-in mathematical function (without even reading any header file at all). Bjarne is referring to such an indication as switch (a compiler-switch).. and there does not have to exist any file with name `cmath` and the compiler, thus, does not have to read any file at all.

On Fri, Jan 12, 2018 at 10:22 PM, <sachin...@gmail.com> wrote:
Hello,
In chapter 9 of C++ programming book by Bjarne Stroustrup, I have come across a statement
"An implementation is allowed to take advantage of knwoledge o the standard library definition to optimize the standard library implementation and the way standard headers are handled. For example, an implementation might have knowledge of the standard math library built in and treat #include <cmath> as a switch that makes the standard math functions available without reading any file"

I couldn't understand how <cmath> can be used as switch and standard math functions be made available without reading cmath header?


Regards,
Sachin

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.

Nicol Bolas

unread,
Jan 14, 2018, 9:43:58 AM1/14/18
to ISO C++ Standard - Discussion, sachin...@gmail.com
On Sunday, January 14, 2018 at 7:09:02 AM UTC-5, sachin...@gmail.com wrote:
Thanks Nawaz. Now I understand this. So you are saying we can explicitly write the declarations of standard headers based on the switch instead of depending on the cmath file.

No. What he's saying is that compilers have the option not to have a "cmath" file somewhere. That compilers could be implemented in such a way as to make `#include <cmath>` not read and parse a C++ file but instead just create a bunch of definitions right there in your code.

It isn't something you personally can do.

sachin...@gmail.com

unread,
Jan 15, 2018, 12:10:46 AM1/15/18
to ISO C++ Standard - Discussion, sachin...@gmail.com
Thanks Nicol for clarifying the things. Probably I did not describe in right way but yes it is something that compiler will be implementing not us.

Can someone please help me understand my second query regarding "Linkage to non C++ code".

Regards,
Sachin

Thiago Macieira

unread,
Jan 15, 2018, 2:40:48 PM1/15/18
to std-dis...@isocpp.org
On Sunday, 14 January 2018 04:09:01 PST sachin...@gmail.com wrote:
> and the solution auother provide is use of extern "C"
>
> Here I don't understand how extern "C" is going to solve the layout of the
> argument put on stack... e.g. On Windows, the argument of a function are
> put on stack from high address to low address (little endian), and on Unix
> the argument of a function are put from low to high address (big endian)

You're wrong. That's not how argument passing works.

And besides, it's not relavant for this mailing list. That's an ABI detail.
Please ask the question you really want to ask in your compiler vendor's
mailing list.

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center

Reply all
Reply to author
Forward
0 new messages