thegreatjedi
thegreatjedi

Reputation: 3038

Why isn't "using std::xxx" needed for <cmath> functions?

For all of the other standard library headers you can include, it is necessary to specify the namespace through any of the following methods:

using namespace std;
using std::xxx;
int main() {
    std::xxx;
}

The sole exception I've encountered so far is in the <cmath> library, when all of their functions I've used thus far do not need any of the above in order to use them without specifying the namespace. Why is that?

Note: I may be wrong that <cmath> is the only standard library header that doesn't need the namespace to be specified, or that every function in <cmath> behaves like this. I just haven't encountered exceptions to what I've said in daily use yet.

Upvotes: 2

Views: 209

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

The C++ Standard allows implementations to place names declared in standard C headers in the global namespace.

From the C++ Standard (17.6.1.2 Headers)

  1. ...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).

Upvotes: 3

Related Questions