Why is my log in the std namespace?

前端 未结 5 2211
闹比i
闹比i 2020-11-27 15:05

In the code below, I define a trivial log function. In main I try not to call it; I call std::log. Nevertheless, my own

5条回答
  •  醉酒成梦
    2020-11-27 15:48

    In C++, the compiler is free to implement the C library in the global namespace and delegate to it (this is implementation defined).

    17.6.1.2.4 Except as noted in Clauses 18 through 30 and Annex D, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in the C standard library (1.2) or the C Unicode TR, as appropriate, as if by inclusion. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations (7.3.3).

    In general, I'd avoid making a function with the same signature as one of the C standard library's. The C++ standard certainly gives compilers the freedom to be using these signatures if it so chooses, which means you may be fighting your compiler if you try to use the same signatures. Hence, you get weird results.

    I would expect a linker error or warning though, and I think it may be worth reporting this.

    [edit]

    Wow, ninja'd.

提交回复
热议问题