Is pow() function defined in the iostream library?

2016-05-22 8:38 am
When I use pow function in my code including only iostream library, it doesn't give any error. I know pow function is a part of the math.h header file but is it also defined in the iostream library? Because apparently the code is working fine with only the iostream included..

回答 (3)

2016-05-22 11:39 am
✔ 最佳答案
I've just tested that and it's not the case, at least with gcc,
<cmath> has to be included. But there are lots of different compilers. You mention <math.h> which is an old header file from C and used in C++98. The new versions of C++, eg C++14, use <cmath>.

Your compiler may be using a built in version of pow. Some compilers have lots of built in functions. gcc includes many builtins but it checks the header files before using them. Some builtins just won't work outside the header files others do.

iostream probably doesn't define pow but it includes other includes. If you ever try and follow all the includes included by other includes you end up opening lot of files. So it may be defined in an included include somewhere else.

What that means is that if you give your C++ file to someone else to compile, it may not compile. If you included cmath then it would compile with any standard compliant C++ compiler. What they guarantee is that if you follow the C++ standard then it will compile. But it might compile if you don't.

If you want you can always look in the <iostream> header file. The C++ in the header files is a bit advanced but they are useful reading to help you learn more about C++.
2016-05-22 9:04 am
No. If you look at the messages from the compiler it will have given you a warning like - Wimplicit-function-declaration... note: include the header <math.h> or explicitly provide a declaration for 'pow'

The compiler may have known what you meant and fetched what it needed or if you had compiled a previous version of the code may have had the function stashed away somewhere. But do not bank on this happening - you should always explicitly state what library/header files you want it to include.
2016-05-22 8:53 am
d


收錄日期: 2021-04-21 18:44:50
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20160522003830AADi7w9

檢視 Wayback Machine 備份