Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Standard Library - std::setenv vs setenv

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?

like image 566
magnus Avatar asked May 17 '15 22:05

magnus


1 Answers

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 getenv shall not introduce a data race (17.6.5.9) provided that nothing modifies the environment. [ Note: Calls to the POSIX functions setenv and putenv modify the environment. — end note ]

from N3797 18.10.5. It is indeed not a C++ standard function, thus not necessarily in namespace std.

like image 151
Baum mit Augen Avatar answered Oct 09 '22 18:10

Baum mit Augen