Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to qualify C functions with the `std` namespace? [duplicate]

When I use functions inherited from C, like the ones in <cmath> or <cstdlib>, should I qualify them as being part of the standard namespace std::log, or should I remain in the C-scope and use them as global functions? What about size_t?

like image 236
Paul Manta Avatar asked Feb 05 '12 12:02

Paul Manta


1 Answers

If you use e.g. <math.h>

No, you shouldn't.

It is unspecified whether they are available in the namespace std on any particular implementation:

[C++11: D.5/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).

However, you should not be using this header:

[C++11: C.3.1/1]: For compatibility with the Standard C library, the C++ standard library provides the 18 C headers (D.5), but their use is deprecated in C++.


If you use e.g. <cmath>

Yes, you should.

It is unspecified whether they are available in the global namespace on any particular implementation:

[C++11: 17.6.1.2/4]: Except as noted in Clauses 18 through 30 and Annex D, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in the C standard library (1.2) or the C Unicode TR, as appropriate, as if by inclusion. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations (7.3.3).

like image 123
Lightness Races in Orbit Avatar answered Sep 19 '22 22:09

Lightness Races in Orbit