Gaslight Deceive Subvert
Gaslight Deceive Subvert

Reputation: 20391

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 deprecated a whole slew of POSIX C functions and replaced them with _-prefixed variants. One example among many is isatty:

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-isatty

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?

Upvotes: 10

Views: 2328

Answers (3)

Steven
Steven

Reputation: 754

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

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.)

Upvotes: 14

Matteo Italia
Matteo Italia

Reputation: 126867

isatty & co., although POSIX, are not standard C, and are provided as "extensions" by the VC++ runtime1.

As such, they are prefixed with an underscore supposedly to avoid name clashes - as names starting with an underscore followed by a lowercase letter are reserved for implementation-defined stuff at global scope. So if, for example, you wanted to use an actual POSIX compatibility layer providing its own versions of these functions, they wouldn't have to fight with the VC++-provided "fake" ones for the non-underscored names.


  1. Extensions which have no presumption to be actually POSIX-compliant, by the way.

Upvotes: 0

MSalters
MSalters

Reputation: 179981

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.

Upvotes: 2

Related Questions