STD and <cmath>

Hello everybody. I have a question. Does function of <cmath> sqrt() or pow() require std:: or using namespace std? If it doesn't, I got no question :D, but if it does need it, why can I use sqrt without std?
In practice, it's rare that it will be a problem, but for full compatibility, you should add the std:: onto the functions in <cmath>. Compiler implementations are not required to put the functions in the global namespace, but most do.

Edit: If you want the actual quote from the standard, see: https://stackoverflow.com/a/11086087
Last edited on
BTW, "STD" means "sexually-transmitted disease", whereas "std" is short for "standard".
The committee really has a thing for bad acronyms... ;)
After all, mathematics is probably the last place you'd find an STD.
Yes, but people are always using their computers for unsafe sex, which is how computer viruses propagate...
> Does function of <cmath> sqrt() or pow() require std:: or using namespace std?

Yes, if we want full C++ functionality.
Even in many implementations, where the C compatible functions are also declared at global namespace scope.

For example:
1
2
3
4
5
6
7
8
9
10
#include <cmath>

int main()
{
    float (*p1)(float) = &std::sqrt ; // fine: resolve overload
    double (*p2)(int) = &std::sqrt ; // fine: instantiate template

    float (*p3)(float) = &sqrt ; // *** error: type mismatch
    double(*p4)(int) = &sqrt ; // *** error: type mismatch
}

http://coliru.stacked-crooked.com/a/bbe3410f654620a2
Note that unix users have 'unistd'. Its catching too, first in one file, then in another...
Topic archived. No new replies allowed.