gpt4 book ai didi

c++ - 为什么 getcwd() 不符合 ISO C++ 标准?

转载 作者:IT老高 更新时间:2023-10-28 22:15:49 25 4
gpt4 key购买 nike

This MSDN article声明 getcwd() 已被弃用,应改用与 ISO C++ 兼容的 _getcwd,这就提出了一个问题:是什么让 getcwd() 不符合 ISO 标准?

最佳答案

有一个good discussion关于那个。 P.J. Plauger这个问题的答案

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.

好吧,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 没有这种可能性。

关于c++ - 为什么 getcwd() 不符合 ISO C++ 标准?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/647704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com