Ahmed Abdelaal
Ahmed Abdelaal

Reputation: 15

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

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?

Upvotes: 1

Views: 410

Answers (2)

Walter
Walter

Reputation: 45464

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.

Upvotes: 8

Joni
Joni

Reputation: 111349

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++.

Upvotes: 3

Related Questions