YelizavetaYR
YelizavetaYR

Reputation: 1701

pow() function working without any math libraries

It seems on some software/compilers the pow() function works without any math libraries at all. Only with <iostream>. But in others it complains. Have math functions been added to the <iostream> library or to some other place?

Upvotes: 6

Views: 1033

Answers (2)

Leontyev Georgiy
Leontyev Georgiy

Reputation: 1315

As it is explicitly noted in here, pow IS in cmath header. http://www.cplusplus.com/reference/cmath/pow/.

What's for includes in <iostream>, just checked that current version of GCC(I'm using archlinux, so it's the latest) doesn't include cmath into any of iostream's inner includes.

Anyway, even if it works, it is against the standard. Include <cmath> explicitly.

Upvotes: 1

eerorika
eerorika

Reputation: 238341

Headers can - and often do - include other headers. Standard library headers are no exception to this.

Even though you chose not to include a header (let's name it a) that you depended on, it's possible that the header happened to be included by another header (let's name it b) that you did include. In that case, your program is not guaranteed to continue working if the b header is ever modified to not include a. That is why you must always include all headers that you depend on - even when your program appears to work without including some of them.

All different versions of different implementations of standard library are different and therefore a in one version could include b while a in another version could just as well not include b. Same applies to all API that have multiple versions of implementations.

Upvotes: 4

Related Questions