cmath vs math.h (And similar c-prefixed vs .h extension headers)

前端 未结 4 687
一整个雨季
一整个雨季 2020-12-08 00:04

I\'ve seen some information about differences between things like iostream vs iostream.h. From what I gathered from those the difference between th

4条回答
  •  佛祖请我去吃肉
    2020-12-08 00:36

    I've seen some information about differences between things like iostream vs iostream.h.

    [iostream.h] is not a standard header.

    it is not an example of the issue you're raising.

    [cmath] defines symbols in the std namespace, and may also define symbols in the global namespace. [math.h] defines symbols in the global namespace, and may also define symbols in the std namespace. if you include the former and use an unqualified symbol, it may compile with one compiler but not with another. therefore it's a good idea to use [math.h]. and in general, for such header pairs, to use the [.h] version.

    c++98 provided a formal guarantee of the cxxx header not polluting the global namespace. maybe that was why they were defined. however, that was a bit harder to implement than polluting ones, so in practice no standard library implementation that i know of followed the standard in this respect, and so it was finally changed to reflect reality in c++11.

提交回复
热议问题