4

What is the namespace for math functions? Global or std?

Consider cos function. It has 3 overloads. But there is also legacy cos from math.h. C doesn't know anything about function overloading. Therefore cos(x) can't be resolved to cos(float). The solution is to call the single precision version explicitly cosf(x). Did I miss anything?

alfC
  • 14,261
  • 4
  • 67
  • 118
pic11
  • 14,267
  • 21
  • 83
  • 119

4 Answers4

4

You get the same functions by including <math.c> and <cmath> in C++, the only differences is the namespace. I.E. including <math.h> also gives you the overload.

In theory, in C++03, using <math.h> gives you the symbols defined in the global namespace and also in the std namespace while using <cmath> gives you the symbols defined in the std namespace and not in the global namespace.

The practice is different and C++ 0X aligned the theory with the practice. <math.h> gives you the symbols defined in the global namespace and perhaps also in the std namespace while using <cmath> gives you the symbols defined in the std namespace and perhaps also in the global namespace.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
  • Actually, the 03 standard doesn't explicitly disallow cmath putting stuff in the global namespace as well, and this is borne out by the fact that then new 0x standard explicitly allows it. – paxdiablo Jun 02 '11 at 14:23
  • @paxdiablo, well looking at http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#206 it seems that I was indeed wrong about C++03. But in the opposite direction, it was even more strict than I though. I'm updating my answer. – AProgrammer Jun 02 '11 at 14:53
  • 1
    It is my recollection that the *intent* in C++03 was, the `` headers should not put things in the global namespace; however, that proved to be very difficult to implement even in contexts where you'd think everyone was cooperating (like gcc with glibc) and also more trouble for people using the headers than it was really worth. I like that C++0x has backed off on that a bit. – zwol Jun 02 '11 at 15:59
  • @AProg, I'm confused. Point 206 of that link has to do with the possible discrepancies between the different `new` operators. How does it relate to namespace population by the two types of headers? And I think the second para is still not quite correct - there is nothing in C++03 stating that cmath is not _allowed_ to place things in global namespace. It just states that it populates std and leaves it at that. – paxdiablo Jun 02 '11 at 16:10
3

They are in the std namespace. But, for backwards compatibility reasons the cmath header also shows them in the global namespace with a using std::cos;.

eduffy
  • 39,140
  • 13
  • 95
  • 92
1

The cXXX headers place all their stuff in the std namespace. They may also put them in the global namespace but it's not required.

This is from C++0x, the upcoming standard, section D.7:

2/ Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).

3/ [ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]

This is unchanged from section D.5 from C++03 (it's made more explicit in the newer standard but the effect is the same):

2/ Every C header, each of which has a name of the form name.h, behaves as if each name placed in the Standard library namespace by the corresponding cname header is also placed within the namespace scope of the namespace std and is followed by an explicit using-declaration.

3/ [Example: The header <cstdlib> provides its declarations and definitions within the namespace std. The header <stdlib.h> makes these available also in the global namespace, much as in the C Standard. —end example]

If you include the 'old-style' XXX.h header, it's placed in both namespaces (in both iterations of the standard).

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • The question is tagged c++ not c++0x – Jay Jun 02 '11 at 14:01
  • 1
    @Jay: Soon enough, there won't be much difference. – Puppy Jun 02 '11 at 14:02
  • 1
    @Jay - Here C++0x merely documents how nearly all C++ compilers already work. In practice there is no difference. – Bo Persson Jun 02 '11 at 14:04
  • So the compiler switches don't do anything? http://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html and I guess the different levels of implementations of a changing standard don't require any thought either? – Jay Jun 02 '11 at 14:56
  • @DeadMG: So I guess all that legacy code is just going to vanish and nobody will ever have to maintain it? – Jay Jun 02 '11 at 14:59
  • @DeadMG @Jay Some of us are still maintaining code that can't use hardly any of the C++03 standard library, never mind *anything* from the newer revision. – zwol Jun 02 '11 at 15:56
0

If you are using C++ you can rely on the function overloading.

There are three version of cos:

double cos (      double x );
float cos (       float x );
long double cos ( long double x );

But if you are using only C or you want portabilty only the double version of this function exists with this name. The float function is cosf.

rsachetto
  • 175
  • 4