Why does the compiler let me call pow and sqrt even though I didn't include cmath?

StackOverflow https://stackoverflow.com/questions/19302222

  •  30-06-2022
  •  | 
  •  

Question

I think the answer should be no, but I wrote some code today that compiled perfectly and showed correct answers using pow and sqrt even though at first I forgot to add #include<cmath> What am I missing here?

Was it helpful?

Solution

Most likely you #included other header files which in turn eventually #included cmath or math.h. edit To answer your question in the comments: #include <iostream> could have done it, that depends on your C++ standard library. The standard does not guarantee it.

But, if you simply declare the functions for yourself, then you can use them too. Though declaring functions in the std namespace for yourself is not standard compliant and cannot be recommended.

OTHER TIPS

The header only includes the function declaration; you can perfectly declare the function without the header and it will work just as well.

It's possible that the header is being included by some other header file you use, without you knowing it.

An implicit declaration, that is, using the function without declaring it, is illegal in C++.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top