c++visual-c++std

is getenv_s not part of cstdlib?


C11 added new bounds-checked functions to the standard library, such as getenv_s.

However, when I include <cstdlib>, I do not have std::getenv_s, only getenv_s (global namespace).

cppreference has the following note:

As with all bounds-checked functions, getenv_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <stdlib.h>.

Even when I define __STDC_WANT_LIB_EXT1__ as 1, My compiler (MSVC C++23) does not find the std::getenv_s function.

image

Isn't <cstdlib> supposed to bring every symbol of <stdlib.h> into the std namespace?


Solution

  • The _s suffix functions are part of the optional Annex K in the C standards. Because they are optional, they may not be supported by all implementations.

    In C++ it is implementation-defined whether including a C++ header will include these functions in the global namespace scope, but they are never made available in the std namespace scope. They are not fully incorporated into the C++ standard library like the non-optional C standard library functions.

    See [headers]/10.


    Also beware that, if they haven't fixed that in the mean time, the MSVC implementation of the _s functions predates those in the standard and sometimes deviates from the standard's specification. See e.g. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm.