我有一个简单的 std::setenv
调用,它在我的 gcc
下的 Linux 发行版上运行良好。但是,在我的 Mac OS X 上使用 clang
时,出现以下错误。
error: no member named 'setenv' in namespace 'std'; did you mean simply 'setenv'?
std::setenv(name.c_str(), value.c_str(), true);
我确定,我在某处读到 setenv
是 C++11 中命名空间 std
的一部分,但现在我不确定。
问题:应该使用setenv
还是std::setenv
,为什么会这样?
最佳答案
我没有find任何类似 std::setenv
的 cppr , 只有 std::getenv
在其文档中引用了 POSIX 函数 setenv
,当然不在 namespace std
中。
因此,由于您没有调用 C++ 标准函数,因此应该采用普通 setenv
方法,因为这是定义函数的标准定义它的方式。请注意,std::setenv
允许(但不是必需)工作。 (另见 hvd's comment。)
对于标准爱好者:setenv
仅作为
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 functionssetenv
andputenv
modify the environment. — end note ]
来自 N3797 18.10.5。它确实不是 C++ 标准函数,因此不一定在 namespace std
中。
关于C++ 标准库 - std::setenv 与 setenv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30292642/