Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Cosine works without the std namespace - Why? [duplicate]

I have a fairly large application and I am working without the std namespace, I noticed I wasn't including std::cos or std::sin yet I am getting the right results. Why?

An example of some cut down code would be:

#include <ctime>
#include <cmath>
#include <iostream>
#include <vector>
//#include <unistd.h>
#include <fstream>
#include <sstream>
#include <iomanip>

using std::cout;
using std::endl;

int main()
{
    double pi = 4*(atan(1));

    cout << "pi = " << pi << endl
         << "cos(pi) = " << cos(pi) << endl
         << "sin(pi) = " << sin(pi) << endl;



    return 0;
}

I have left all the headers in, I am using them all in the main code. The output returns ~3.14, -1 and 1e-16 as expected. Why does this work? cos and sin are in std aren't they?

I'm using the g++ compiler on a remote unix server

Thanks

like image 469
Aaron Avatar asked Jan 03 '14 12:01

Aaron


1 Answers

When you include <cmath>, all of the functions are declared in std::. For the C headers, there is also a special rule, which allows (but doesn't require) the implementation to make them visible in the global namespace; this is because most implementations will simply adapt the C headers, something like:

#include <math.h>
namespace std
{
    using ::sin;
    using ::cos;
    // ...
}

This is an obvious way of implementing the library without having to rewrite everything, just to have it in C++, and it will result in all of the names also being present in the global namespace.

Formally, this is a C++11 feature; pre-C++11 required that <cmath> only introduce the symbols into std::. Practically, all, or at least most implementations did something like the above, and did introduce them, illegally, into the global namespace, so C++11 changed the standard to reflect reality.

like image 112
James Kanze Avatar answered Nov 15 '22 16:11

James Kanze