Search code examples
c++includecmath

How is it possible to use pow without including cmath library


I'm trying to learn C++ and I'm using MS Visual Studio 2019. I have the code below:

#include <iostream>

int main()
{
    std::cout << pow(10, 2);
}

How is it possible to compile and run without error without including cmath? Inside the solution there is only one file with the above code.


Solution

  • How is possible in C++ to use pow without include cmath library

    By including another header that includes the <math.h> header.

    There is no guarantee that standard library headers won't include other headers in general, nor that <iostream> won't include <cmath> in particular. Nor is there a guarantee that <iostream> will include that header, so this program may fail to compile when using another standard library implementation or another version of the same.

    In concluson: Never rely on such transitive inclusion. Always directly include all of the headers whose declarations you depend on, unless the transitive include is explicitly doumented (for example, <ios> is guaranteed to include <iosfwd>). You cannot use successful compilation as proof that you have provided all of the required direct inclusions.