为什么 getcwd() 不是 ISO C++ 符合吗?

发布于 2024-07-16 01:44:36 字数 182 浏览 8 评论 0原文

这篇 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

人疚 2024-07-23 01:44:36

关于此问题有一个很好的讨论PJ Plauger对此的回答

我是那个在 1983 年就坚持认为空间的人
C 程序可用的名称可分为:

a) 那些为了程序员的利益而由实现定义的(例如 printf)
b) 保留给程序员的(例如 foo)
c) 保留给实现的(例如 _unlink)

我们甚至在那时就知道“实现”过于单一——
通常不止一个来源提供部分实现——
但这是我们当时能做的最好的事情了。 标准C++
引入了命名空间来提供帮助,但他们只实现了
他们既定目标的一小部分。 (这就是当你
标准化纸老虎。)

在这种特殊情况下,Posix 提供了类别 (a) 名称的列表
(例如取消链接),当且仅当您
包括某些标头。 由于 C 标准窃取了其标头
Unix,与 Posix 具有相同的来源,其中一些标头
历史上有重叠。 尽管如此,编译器警告应该有
某种方式考虑是否受支持的环境
是“纯”标准 C++(柏拉图理想)或混合 C/C++/Posix
环境。 微软目前为帮助我们穷人所做的尝试
程序员没有考虑到这一点。 它坚持治疗
unlink 作为类别 (b) 名称,这是短视的。

好吧,GCC 至少不会在严格的 C 模式下声明 POSIX 名称(尽管在 C++ 模式下仍然如此):

#include <stdio.h>

int main() {
    &fdopen;
    return 0;
}

使用 -std=c99 输出

test.c: In function 'main':
test.c:4: error: 'fdopen' undeclared (first use in this function)

您必须明确告诉它您正在操作在混合 C/Posix 中,通过使用功能测试宏或不通过任何特定标准。 然后,它将默认为 gnu89,它假定混合环境 (man feature_test_macros)。 显然,MSVC不具备这种可能性。

There is a good discussion about that. P.J. Plauger answers to this

I'm the guy who insisted back in 1983 that the space of
names available to a C program be partitioned into:

a) those defined by the implementation for the benefit of the programmer (such as printf)
b) those reserved to the programmer (such as foo)
c) those reserved to the implementation (such as _unlink)

We knew even then that "the implementation" was too monolithic --
often more than one source supplies bits of the implementation --
but that was the best we could do at the time. Standard C++
has introduced namespaces to help, but they have achieved only
a fraction of their stated goals. (That's what happens when you
standardize a paper tiger.)

In this particular case, Posix supplies a list of category (a) names
(such as unlink) that you should get defined when and only when you
include certain headers. Since the C Standard stole its headers from
Unix, which is the same source as for Posix, some of those headers
overlap historically. Nevertheless, compiler warnings should have
some way of taking into account whether the supported environment
is "pure" Standard C++ (a Platonic ideal) or a mixed C/C++/Posix
environment. The current attempt by Microsoft to help us poor
programmers fails to take that into account. It insists on treating
unlink as a category (b) name, which is myopic.

Well, GCC will not declare POSIX names in strict C mode, at least (though, it still does in C++ mode):

#include <stdio.h>

int main() {
    &fdopen;
    return 0;
}

Output using -std=c99

test.c: In function 'main':
test.c:4: error: 'fdopen' undeclared (first use in this function)

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.

无畏 2024-07-23 01:44:36

标准中未指定的函数应该以下划线为前缀,以表明它们是特定于供应商的扩展或遵守非 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.

当爱已成负担 2024-07-23 01:44:36

根据记录,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 a max_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.

痞味浪人 2024-07-23 01:44:36

正如其他人已经指出的那样, 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).

み格子的夏天 2024-07-23 01:44:36

要添加 Dan Olson 的帖子:请参阅 ANSI C 合规性页面微软软件定义网络

Microsoft 特定函数和全局变量的名称以单个下划线开头。 这些名称只能在代码范围内本地覆盖。 例如,当您包含 Microsoft 运行时头文件时,您仍然可以通过声明同名的本地变量来本地覆盖名为 _open 的 Microsoft 特定函数。 但是,您不能将此名称用于您自己的全局函数或全局变量。

To add on to Dan Olson's post: See ANSI C Compliance page on MSDN

The names of Microsoft-specific functions and global variables begin with a single underscore. These names can be overridden only locally, within the scope of your code. For example, when you include Microsoft run-time header files, you can still locally override the Microsoft-specific function named _open by declaring a local variable of the same name. However, you cannot use this name for your own global function or global variable.

书间行客 2024-07-23 01:44:36

据我所知, 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.

坦然微笑 2024-07-23 01:44:36

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文