Deprecated Standard C-Library and Overloaded C ++ Functions

C ++ language standard speaks in D.5

2 Each C header, each of which has a name of the form name.h , behaves as if each name was placed in the standard library namespace; the corresponding cname header is placed in the global namespace volume. It is not indicated whether these names are first declared or defined in the namespace (3.3.6) of the std and then explicitly used-declarations (7.3.3) are entered into the global namespace realm.

3 [Example: the <cstdlib> header undoubtedly provides its declarations and definitions within the std . It can also provide these names in the global namespace. The <stdlib.h> header undoubtedly provides the same declarations and definitions in the global namespace as in Standard C. It can also provide these names in the std . -end example]

This is apparently quite explicit ("... every name ...", "... the same declarations ...") that the old-style <name.h> headers should provide the same set of declarations as and the <cname> headers of the new style, but in the global namespace. An exception for C ++ - specific overloaded versions of various C-functions, for example, is not made.

This means that <math.h> should provide three versions of the sin function: sin(float) , sin(double) and sin(long double) in the global namespace. This, in turn, means that the following C ++ code must have overload permission

 #include <math.h> int main() { sin(1); } 

It crashes with the MSVC ++ compiler, but it compiles successfully in GCC and Clang. So, does GCC just ignore the standard requirement for legacy old-style headers? Or did I somehow misinterpret the wording in the standard?

+5
source share
1 answer

Thanks to the comments of @hvd, I saw the light, it turns out that the MSVC is correct, and the GCC should also complain about this ambiguity.

The only differences between including <cmath> vs <math.h> are that the names initially have a scope that is in the namespace std for the former, and the global namespace for the latter (implementations can provide names in a different namespace, but this not required), but the fact that the inclusion of .h options for C. headers is deprecated.

+2
source

Source: https://habr.com/ru/post/1206373/


All Articles