cmath header confusion

前端 未结 4 1824
深忆病人
深忆病人 2020-12-11 21:00

What is the namespace for math functions? Global or std?

Consider cos function. It has 3 overloads. But there is also legacy cos from math.

4条回答
  •  不知归路
    2020-12-11 21:25

    The cXXX headers place all their stuff in the std namespace. They may also put them in the global namespace but it's not required.

    This is from C++0x, the upcoming standard, section D.7:

    2/ Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).

    3/ [ Example: The header assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]

    This is unchanged from section D.5 from C++03 (it's made more explicit in the newer standard but the effect is the same):

    2/ Every C header, each of which has a name of the form name.h, behaves as if each name placed in the Standard library namespace by the corresponding cname header is also placed within the namespace scope of the namespace std and is followed by an explicit using-declaration.

    3/ [Example: The header provides its declarations and definitions within the namespace std. The header makes these available also in the global namespace, much as in the C Standard. —end example]

    If you include the 'old-style' XXX.h header, it's placed in both namespaces (in both iterations of the standard).

提交回复
热议问题