(probably trivial) question about cmath library

F

Fab

Dear all,

I am a little confused about the std namespace used for all c++ libs.
I am using g++ for compilation and I think my issue could also be
related to g++ rather than the c++ libs. Here's my issue:

When I use #include <iostream> in my code, I have to use the scope
operator or global the namespace std to access e.g. cout. However, when
I use #include <cmath> and want to use e.g. cos from that lib, the above
mentioned does somehow not apply. Why?

Thank you,
Fab
 
S

SG

When I use #include <iostream> in my code, I have to use the scope
operator or global the namespace std to access e.g. cout.  However, when
I use #include <cmath> and want to use e.g. cos from that lib, the above
mentioned does somehow not apply.  Why?

std::cos is guaranteed to work, unqualified cos is not guaranteed to
work. If it works for you, chances are that cmath is implemented in
terms of the C header math.h like this:

#ifndef STD_CMATH_INCLUDED
#define STD_CMATH_INCLUDED

#include <math.h>

namespace std {
...
using ::cos;
...
}

#endif

But a C++ compiler vendor might implement this differently in a way
that only introduces math functions in std namespace and NOT the
global scope (at least up to but excluding C++2011, I don't know if
this changed in C++2011).
Thank you,
Fab

Cheers!
SG
 
J

Juha Nieminen

Fab said:
When I use #include <iostream> in my code, I have to use the scope
operator or global the namespace std to access e.g. cout. However, when
I use #include <cmath> and want to use e.g. cos from that lib, the above
mentioned does somehow not apply. Why?

What's happening with gcc in particular (and possibly with other
compilers) is that, AFAIK, <cmath> internally also includes <math.h>
(which is the C equivalent), which in turn has the math functions in
the global namespace.

Note that if you use the unqualified math function names, you will
be calling the 'double' versions of <math.h> instead of the type-specific
versions in <cmath> (which are inside the std namespace). This can make
a difference if you are eg. using long doubles.

(Also, using the unqualified names is not guaranteed to be portable.)
 
M

Marc

SG said:
std::cos is guaranteed to work, unqualified cos is not guaranteed to
work. If it works for you, chances are that cmath is implemented in
terms of the C header math.h like this:
[...]

which most libraries have been illegally doing since the beginning.
But a C++ compiler vendor might implement this differently in a way
that only introduces math functions in std namespace and NOT the
global scope

as required by C++03.
(at least up to but excluding C++2011, I don't know if
this changed in C++2011).

C++11 makes the almost universal implementation you described above
legal (except for a few details) but doesn't forbid the old one.

The description (basically what you said):
* math.h provides ::cos
* cmath provides std::cos
* either may provide the other, but you can't rely on it
remains the safest.
 
M

Marc

Juha said:
What's happening with gcc in particular (and possibly with other
compilers) is that, AFAIK, <cmath> internally also includes <math.h>
(which is the C equivalent), which in turn has the math functions in
the global namespace.

Note that if you use the unqualified math function names, you will
be calling the 'double' versions of <math.h> instead of the type-specific
versions in <cmath> (which are inside the std namespace). This can make
a difference if you are eg. using long doubles.

Note that this is a gcc bug (a common one, so you might as well
consider it standard to be on the safe side...). math.h is also
supposed to provide all the overloads in C++ (not sure if that
requirement will still be there in C++1y).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top