How is it possible to use pow without including cmath library

血红的双手。 提交于 2020-05-30 13:03:30

问题


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.


回答1:


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.




回答2:


Update: Concerned by a comment by @Pete Becker, I correct my answer to that:

Your code is, ill formed and by that invokes so called undefined behavior not implementation defined behavior.

However this conclusion is not completely safe, since we enter the part of language lawyering here.

Undefined behavior (UB) is defined as

behavior for which this International Standard imposes no requirements which usually means: Don't try to reason about this code and never use it.

The question is, does C++ specify requirements on your code i.e. is your code a well-formed program? Then this would be specified behavior (the usual behavior),unspecified behavior or even the qualification of that implementation defined behavior. But in in 20.5.2.2 Headers [using.headers] the C++ standard says

A translation unit shall include a header only outside of any declaration or definition, and shall include the header lexically before the first reference in that translation unit to any of the entities declared in that header. No diagnostic is required.

According to Difference between Undefined Behavior and Ill-formed, no diagnostic message required this would imply undefined behavior, even though it does not directly violate the 6.2 One-definition rule, which would have to be violated in your case to be a ill-formed program, but as the answer to the linked question already stated that this rule defaults to undefined behavior, if there is not exactly one definition, we have an ill formed program here.


Old part: Simple but unsatisfactory answer: So called implementation defined behavior. The Microsoft Visual C++ compiler and its headers are free to already include each other, probably MSVCs iostream header is already including cmath in an at least transitive way if not directly.

The fun begins if you try to compile the same code with gcc or clang. Sudden missing includes are the most common portable errors of my code at least.

If you actually include every header that the standard says it required i.e. contains the definitions of what you are using you get so called portable code, meaning code that will compile and do the same regardless of using MSVC/Visual Studio or gcc or clang or any other standard conformant compiler.



来源:https://stackoverflow.com/questions/61324712/how-is-it-possible-to-use-pow-without-including-cmath-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!