C ++标准库-std :: setenv vs setenv [英] C++ Standard Library - std::setenv vs setenv

查看:232
本文介绍了C ++标准库-std :: setenv vs setenv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对std::setenv有一个简单的调用,该调用在gcc下的Linux发行版上可以正常使用.但是,在Mac OS X上使用clang时,出现以下错误.

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

我确定,我已经读过某个地方说setenv是C ++ 11中命名空间std的一部分,但是现在我不确定.

I'm sure, I've read somewhere that setenv was part of namespace std in C++11, but now I'm unsure.

问题:应该使用setenv还是std::setenv,为什么会这样?

Question: Should setenv or std::setenv be used, and why is this the case?

推荐答案

我不 cppr ,只有 std::getenv 在其文档中引用了POSIX函数 setenv ,当然不是 namespace std中.

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.

因此,由于您没有调用C ++标准函数,因此应该使用纯setenv,因为这是定义函数的标准定义它的方式.请注意,允许(但不是必需)std::setenv起作用. (另请参见 hvd的评论.)

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

对于标准爱好者:setenv仅被提及为

For the standard-lovers: setenv is only mentioned as

对函数getenv的调用不得引入数据竞争(17.6.5.9),前提是没有任何修改. 环境. [注意:调用POSIX函数setenvputenv会修改环境. - 结尾 注意]

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 ]

来自N3797 18.10.5.它确实不是C ++标准函数,因此不一定在namespace std中.

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

这篇关于C ++标准库-std :: setenv vs setenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆