为什么《男人2》会开?话说有两种打开方式吗?

发布于 2024-09-28 11:54:43 字数 239 浏览 3 评论 0原文

我在输入 man 2 open 时遇到了这个问题。它说有两种打开方式,一种有两个参数,一种有三个!上次我检查时我们无法重载 C 中的函数。他们是如何做到这一点的?他们是用C++写的吗?

int open(const char * pathname, int flags);
int open(const char * pathname, int flags, mode_t mode);

I ran into this question while typing man 2 open. It says that there are two kinds of open, one with two args, and one with three! last time i checked we could not overload functions in C. How did they do this? did they write in C++?

int open(const char * pathname, int flags);
int open(const char * pathname, int flags, mode_t mode);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

神魇的王 2024-10-05 11:54:43

不,他们只是使用了可变参数函数。

int open(const char * pathname, int flags, ...);

这使得最后一个参数 mode 可选。原型仅显示该功能应如何使用,而不是实际的界面。

当然,与真正的重载不同,编译器无法对 mode 参数进行类型检查,因此用户必须格外小心,确保只传递 2 或 3 个参数,并且第三个参数必须是 <代码>mode_t。


顺便说一句,如果您检查 BSD(包括 OS X)的 man 2 open ,它会显示 正确的原型如上

No, they just used variadic function.

int open(const char * pathname, int flags, ...);

This makes the last argument mode optional. The prototypes only show how the function should be used, not the actual interface.

Of course, unlike real overloading, the compiler cannot type-check the mode argument, so the user have to be extra careful to ensure only 2 or 3 arguments are passed, and the 3rd argument must be a mode_t.


BTW, if you check the man 2 open for BSD (including OS X) it shows the correct prototype as above.

你的往事 2024-10-05 11:54:43

无论如何,手册页是不正确的。它显示的原型:

int open(const char * pathname, int flags);
int open(const char * pathname, int flags, mode_t mode);

等同于正确的原型:

int open(const char * pathname, int flags, ...);

使用它提供的不正确的原型(例如,如果您自己创建函数原型而不是包含正确的标头)将导致您的程序出现未定义的行为。 (这不仅仅是理论上的;它可能无法在 x86_64 和其他具有传递寄存器 ABI 的平台上运行。)

手册页试图表达(并且做得非常糟糕)的是可变参数部分open 的参数列表可以为空或单个 mode_t 类型参数,其中可选参数的缺失或存在取决于 flags< 的值/代码>。

For what it's worth, the man page is incorrect. The prototypes it shows:

int open(const char * pathname, int flags);
int open(const char * pathname, int flags, mode_t mode);

are not equivalent to the correct prototype:

int open(const char * pathname, int flags, ...);

Using the incorrect ones it provides (e.g. if you prototype the function yourself rather than including the right header) will cause your program to have undefined behavior. (And this is not just theoretical; it will probably fail to run on x86_64 and other platforms with pass-by-register ABIs.)

What the man page was trying to express (and doing a very poor job of) is that the variadic part of open's argument list can be either empty or a single mode_t type argument, where the absence or presence of the optional argument depends on the value of flags.

脸赞 2024-10-05 11:54:43

不,他们用 C 语言编写,使用可变参数。

查看 stdarg.h,其中有示例。

可变参数函数可以从 省略号 中读取任意数量的参数 (...) 。事实上,函数“不需要”的任何额外参数都被丢弃。

No, they wrote in C, using varargs.

Check out stdarg.h, where there are examples.

A variadic function may read as many arguments as it likes from the ellipsis (...) . Any extra arguments the function "does not want" are in fact discarded.

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