Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Microsoft's underscore C functions?

This question is about the same subject as strdup or _strdup? but it is not the same. That question asks how to work around MS's renamings, this question asks why they did it in the first place.

For some reason Microsoft has taken a whole slew of POSIX C functions and have deprecated and replaced them with _-prefixed variants. One example of many is isatty:

https://msdn.microsoft.com/en-us/library/ms235388.aspx

This POSIX function is deprecated. Use the ISO C++ conformant _isatty instead.

What exactly is ISO C++ conformant about _isatty? It appears to me that the MSDN help is totally wrong.

The other questions answer explained how to deal with this problem. You add the _CRT_NONSTDC_NO_DEPRECATE define. Fine. But I want to know what Microsoft's thinking is. What was their point in renaming and deprecating functions? Was it just to make C programmers lives even harder?

like image 741
Björn Lindqvist Avatar asked Jun 15 '16 20:06

Björn Lindqvist


2 Answers

The fact that _isatty() is ISO C++ conformant makes sense if you think of it like a "language lawyer".

Under ISO C++, the compiler is only supposed to provide the functions in the standard (at least for the standard headers) -- they're not allowed to freely add extra functions, because it could conflict with functions declared in the code being compiled. Since isatty() is not listed in the standard, providing an isatty() function in a standard header would not be ISO C++ compliant.

However, the standard does allow the compiler to provide any function it wants as long as the function starts with a single underscore. So -- language lawyer time -- _isatty() is compliant with ISO C++.

I believe that's the logic that leads to the error message being phrased the way it is.

(Now, in this specific case, isatty() was provided in io.h, which is not actually a C++ standard header, so technically Microsoft could provide it and still claim to be standards-conformant. But, they had other non-compliant functions like strcmpi() in string.h, which is a standard header. So, for consistency, they deprecated all of the POSIX functions the same way and they all report the same error message.)

like image 129
Steven Avatar answered Oct 04 '22 06:10

Steven


Names starting with an underscore, like _isatty are reserved for the implementation. They do not have a meaning defined by ISO C++, nor by ISO C, and you can't use them for your own purposes. So Microsoft is entirely right in using this prefix, and POSIX is actually wrong.

C++ has namespaces, so a hypthetical "Posix C++" could define namespace posix, but POSIX has essentially become fossilized - no new innovation in that area.

like image 26
MSalters Avatar answered Oct 04 '22 06:10

MSalters