gpt4 book ai didi

c++ - 为什么不能在 C++ 中将 putc() 实现为宏? (或者可以吗?)

转载 作者:行者123 更新时间:2023-12-03 10:05:30 25 4
gpt4 key购买 nike

阅读en.cppreference.com在 fputc()/putc() 上,我偶然发现了以下语句:

In C, putc() may be implemented as a macro, which is disallowed in C++.


这是真的?如果是这样,这是在哪里(在 C++ 标准中)说明的?
可能相关: https://stackoverflow.com/questions/10712423

最佳答案

putc在 C 标准中被指定为一个函数:

7.19.7.8 The putc function


C 标准允许将任何 C 标准库函数实现为宏(仍然需要提供真正的函数实现,但如果在头文件中定义了宏,则将使用该宏):

7.1.4 Use of library functions

Any function declared in a header may be additionally implemented as a function-like macro


但是 C++ 标准明确指出这些必须定义为函数:

17.4.1.2.6 Headers

Names that are defined as functions in C shall be defined as functions in the C++ Standard Library.


脚注明确说明不允许额外的宏定义:

This disallows the practice, allowed in C, of providing a "masking macro" in addition to the function prototype. The only way to achieve equivalent "inline" behavior in C++ is to provide a definition as an extern inlin

关于c++ - 为什么不能在 C++ 中将 putc() 实现为宏? (或者可以吗?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65983504/

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