I have a simple call to std::setenv, which works fine on my Linux distribution under gcc. However, when using clang on my Mac OS X, I get the following error.
error: no member named 'setenv' in namespace 'std'; did you mean simply 'setenv'?
std::setenv(name.c_str(), value.c_str(), true);
I'm sure, I've read somewhere that setenv was part of namespace std in C++11, but now I'm unsure.
Question: Should setenv or std::setenv be used, and why is this the case?
I do not find anything like std::setenv on cppr, only std::getenv which in its documentation references the POSIX function setenv, which of course is not in namespace std.
So since you are not calling a C++ standard function, plain setenv should be the way to go since this is how the standard that defines the function defined it. Note that std::setenv is allowed (but not required) to work. (See also hvd's comment.)
For the standard-lovers: setenv is only mentioned as
Calls to the function
getenvshall not introduce a data race (17.6.5.9) provided that nothing modifies the environment. [ Note: Calls to the POSIX functionssetenvandputenvmodify the environment. — end note ]
from N3797 18.10.5. It is indeed not a C++ standard function, thus not necessarily in namespace std.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With