Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #15415 > unrolled thread
| Started by | Fab <fab4100@gmail.com> |
|---|---|
| First post | 2012-04-17 08:06 +0200 |
| Last post | 2012-04-17 07:11 +0000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.c++
(probably trivial) question about cmath library Fab <fab4100@gmail.com> - 2012-04-17 08:06 +0200
Re: (probably trivial) question about cmath library SG <s.gesemann@gmail.com> - 2012-04-16 23:18 -0700
Re: (probably trivial) question about cmath library Marc <marc.glisse@gmail.com> - 2012-04-17 07:00 +0000
Re: (probably trivial) question about cmath library Juha Nieminen <nospam@thanks.invalid> - 2012-04-17 06:58 +0000
Re: (probably trivial) question about cmath library Marc <marc.glisse@gmail.com> - 2012-04-17 07:11 +0000
| From | Fab <fab4100@gmail.com> |
|---|---|
| Date | 2012-04-17 08:06 +0200 |
| Subject | (probably trivial) question about cmath library |
| Message-ID | <877gxe99jj.fsf@gmail.com> |
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
[toc] | [next] | [standalone]
| From | SG <s.gesemann@gmail.com> |
|---|---|
| Date | 2012-04-16 23:18 -0700 |
| Message-ID | <95fb8776-77dd-40de-b1dc-5bd1c6e662e7@fv28g2000vbb.googlegroups.com> |
| In reply to | #15415 |
On Apr 17, 8:06 am, Fab wrote:
>
> 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
[toc] | [prev] | [next] | [standalone]
| From | Marc <marc.glisse@gmail.com> |
|---|---|
| Date | 2012-04-17 07:00 +0000 |
| Message-ID | <jmj4dv$4ls$1@news-rocq.inria.fr> |
| In reply to | #15416 |
SG wrote: > On Apr 17, 8:06 am, Fab wrote: >> >> 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: [...] 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.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2012-04-17 06:58 +0000 |
| Message-ID | <4f8d1489$0$3101$7b1e8fa0@news.nbl.fi> |
| In reply to | #15415 |
Fab <fab4100@gmail.com> wrote: > 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.)
[toc] | [prev] | [next] | [standalone]
| From | Marc <marc.glisse@gmail.com> |
|---|---|
| Date | 2012-04-17 07:11 +0000 |
| Message-ID | <jmj52i$4ls$2@news-rocq.inria.fr> |
| In reply to | #15418 |
Juha Nieminen wrote: > Fab <fab4100@gmail.com> wrote: >> 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. 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).
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web