为什么 getcwd() 不是 ISO C++ 符合吗?
这篇 MSDN 文章 指出 getcwd() 已被弃用,并且 ISO应改用 C++ 兼容的 _getcwd,这就提出了一个问题:是什么使得 getcwd() 不符合 ISO 标准?
This MSDN article states that getcwd() has been deprecated and that the ISO C++ compatible _getcwd should be used instead, which raises the question: what makes getcwd() not ISO-compliant?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
关于此问题有一个很好的讨论 。 PJ Plauger对此的回答
好吧,GCC 至少不会在严格的 C 模式下声明 POSIX 名称(尽管在 C++ 模式下仍然如此):
使用
-std=c99
输出您必须明确告诉它您正在操作在混合 C/Posix 中,通过使用功能测试宏或不通过任何特定标准。 然后,它将默认为
gnu89
,它假定混合环境 (man feature_test_macros
)。 显然,MSVC不具备这种可能性。There is a good discussion about that. P.J. Plauger answers to this
Well, GCC will not declare POSIX names in strict C mode, at least (though, it still does in C++ mode):
Output using
-std=c99
You will have to tell it explicitly that you are operating in a mixed C/Posix by using feature test macros or not passing any specific standard. It will then default to
gnu89
which assumes a mixed environment (man feature_test_macros
). Apparently, MSVC does not have that possibility.标准中未指定的函数应该以下划线为前缀,以表明它们是特定于供应商的扩展或遵守非 ISO 标准。 因此,此处的“合规性”是 Microsoft 在该特定函数的名称中添加下划线,因为它不是 ISO 标准的一部分。
Functions not specified in the standard are supposed to be prefixed by an underscore as an indication that they're vendor-specific extensions or adhere to a non-ISO standard. Thus the "compliance" here was for Microsoft to add an underscore to the name of this specific function since it's not part of the ISO standard.
根据记录,ISO 并未弃用
getcwd()
。 它已被微软“弃用”。 Microsoft 重写了许多 C 函数——通常会考虑到更好的安全性(例如,也采用max_length
参数的字符串函数)。 然后他们让编译器吐出这些警告,我认为这些警告是假的,因为没有标准组弃用任何声明弃用的函数。For the record,
getcwd()
wasn't deprecated by ISO. It was "deprecated" by Microsoft. Microsoft rewrote many C functions -- often with a little better security in mind (say, string functions that also take amax_length
parameter). They then had their compiler spit out these warnings, which I consider bogus because no standards group deprecated any of the functions declared deprecated.正如其他人已经指出的那样, getcwd 不包含在 ISO C++ 中,而是 POSIX/IEEE 标准 1003.1。
Microsoft 已决定在其 C 标准库中包含一些最常用的 POSIX 函数(但在这些函数前添加下划线以从根本上阻止其使用)。
As others have already pointed out, getcwd is not included in ISO C++, but is part of POSIX/IEEE Std 1003.1.
Microsoft has decided to include some of the most commonly used POSIX functions in their C standard library (but prefix these functions with an underscore to essentially discourage their usage).
要添加 Dan Olson 的帖子:请参阅 ANSI C 合规性页面微软软件定义网络
To add on to Dan Olson's post: See ANSI C Compliance page on MSDN
据我所知, getcwd() 从未成为 ISO 标准 C++ 的一部分。 _getcwd() 绝对不是,因为标准名称不会以下划线开头。
事实上,MSDN 文章链接到一个手册页,其中显示它是在 direct.h 中声明的,而它不是标准 C++ 头文件。 这篇文章对我来说似乎是假的。
As far as I'm aware getcwd() has never been part of ISO Standard C++. _getcwd() definitely isn't, as standard names will not begin with an underscore.
In fact, the MSDN article links to a man page that says it is declared in direct.h, which is not a Standard C++ header file. The article seems bogus to me.
MSDN 文章对于普通人快速阅读后得出的结论有些令人困惑(如果他们没有以非常仔细的律师眼光来阅读)。
MSDN 文章说的是: getcwd() 不符合 ISO C++ 标准。 为了遵守函数命名的 ISO C++ 标准(这是 getcwd 所违反的),Microsoft 正确地在函数前面放置了一个 _,因此相同的函数变成了 _getcwd()。 这是符合 ISO C++ 的函数命名方式,因为 getcwd() 和 _getcwd() 不是 ISO C++ 标准函数,而是 Microsoft(供应商)特定或特定于实现的函数。
这篇文章没有指出获取工作目录的 C++ ISO 标准调用是什么……尽管人们往往会快速浏览一下。
The MSDN article is somewhat confusing in what a normal person would conclude from just a quick reading (if they don't read it with a very careful lawyer eye).
What the MSDN article says is: getcwd() is not compliant with the ISO C++ standard. To comply with that ISO C++ standard for naming of functions (which is what getcwd violates), Microsoft properly put an _ on the front of the function, so the same function becomes _getcwd(). That is the ISO C++ compliant way of naming the function because getcwd() and _getcwd() are not an ISO C++ standard function, but are a Microsoft (vendor) specific, or implementation specific function.
The article does not indicate what a C++ ISO standard call to get the working directory would be... though thats what folks tend to read at a quick glance.