-6

Why do I need to include <cmath> to use the function pow() and and also need to declare using namespace std?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
George
  • 9
  • 2
  • 5
    You don't need `using namespace std` and [you shouldn't be using it](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?rq=1). And why do you need to include ``? Well, because `std::pow()` function is declared there. Compiler needs to know the declaration before you can use a certain function in your code, and for that reason you `#include` other header files. – Yksisarvinen Aug 15 '19 at 23:20
  • it isnt clear what you are asking. you need to qualify with a namespace when something is in a namespace. – jwezorek Aug 15 '19 at 23:28
  • Yes, I know is better than use std::pow() , but why I need to use std and not only pow() without std::? – George Aug 15 '19 at 23:29
  • Because the linker (a program that composes the compilation process) could not find the name `pow`. For the linker to proper know what is this name `pow` you are saying about it need you to specify where the name is actually declared. It is just how the language works. – andresantacruz Aug 15 '19 at 23:32
  • Because `std::pow` is in the `std` namespace? That's what namespaces do. If you think `std::pow` should be in the global namespace because it's from the C standard library, it's simply different in C++. – eesiraed Aug 15 '19 at 23:37
  • Three answers in the comments section. – Lightness Races in Orbit Aug 16 '19 at 01:52
  • Tiny question with two spelling mistakes - please take more care. – Lightness Races in Orbit Aug 16 '19 at 01:55
  • I don't think the cited dup was a good choice for this question. This question may be a duplicate of another question, but that's a different matter. Also see [Why < cstdlib > is more complicated than you might think](https://developers.redhat.com/blog/2016/02/29/why-cstdlib-is-more-complicated-than-you-might-think/) on the Red Hat blogs. `pow` is likely the same as `log` with its three overloads for `float`, `double` and `long double`. – jww Aug 16 '19 at 05:33

2 Answers2

3

In C++ functions can be declared inside namespaces. Namespaces are just what its name infer, a collection of names.

To access a name (let's say the function pow()) that is declared inside the namespace std, you can access it in 2 ways: using the namespace access prefix std:: (i.e. std::pow()), or you can declare using namespace std. However as @Yksisarvinen mentioned, you should avoid the use of using namespace.

andresantacruz
  • 1,676
  • 10
  • 17
  • I prefer to not say: "avoid `using namespace`" but just advice the OP about common issues with. You can scope `usings` inside a function, for example, and can be worth. – Kevin Kouketsu Aug 16 '19 at 00:37
  • Arguably it may infer the same problem than declaring the `using namespace` statement in a .cpp. It is better to just don't use it. – andresantacruz Aug 16 '19 at 00:52
  • No, there is nothing wrong with using it in a block scope. The arguments against do not fit there. – Lightness Races in Orbit Aug 16 '19 at 01:51
  • @LightnessRacesinOrbit could you elaborate why the same argument against using this statement isn't valid inside a function? – andresantacruz Aug 16 '19 at 01:53
  • Why would it be? How do you leak names into other headers and throughout multiple functions and definitions and bits of code ... if you're constraining it to your one tiny little block? That's the whole point of blocks. – Lightness Races in Orbit Aug 16 '19 at 01:55
  • What about name resolution in the overall code? Function `foo`is calling function `myfunc` in `namespace `zoo` and in the future someone creates another `myfunc` in the same `obj` or in another `obj` in global namespace; wouldn't it be problematic on this regard? – andresantacruz Aug 16 '19 at 02:04
0

It is possible that someone else (even you) would define another function pow() visible from the same module. This will bring the ambiguity for the compiler (which function to call?) and possibly to the linker (if there are two functions with exactly the same names, how it would distinguish them?). This problem could be solved by putting the functions into namespaces. For example, if you create a namespace george and define a function pow() there, the linker would separate these functions, and the compiler would get a chance to resolve the ambiguity.

The using namespace std is a non-recommended shortcut that simplifies the usage of standard functions and classes in exchange to the possible ambiguity. Using the full name however would resolve it even in this case.

Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40