为什么cstdio undef或其他函数

why cstdio undef gets or other functions

本文关键字:其他 函数 undef cstdio 为什么      更新时间:2023-10-16

当我阅读libstdc++-v3/include/c/cstdio时,我对下面的行很困惑:

//Get rid of those macros defined in <stdio.h> in lieu of real functions.
#undef getc
#undef gets

我认为getc是一个宏,但gets是一个函数,事实上,为什么不定义一个函数?可能在某些系统中,它都是在宏中实现的?


更新:我试着把我对<stdio.h><cstdio> here的理解

<stdio.h>=
__BEGIN_NAMESPACE_STD
extern int fgetc (FILE *__stream);
extern int getc (FILE *__stream);
__END_NAMESPACE_STD
#define getc(_fp) _IO_getc (_fp)
<cstdio>=
#undef getc
  • 宏可能有一些副作用,导致我们不使用,但我不能为上面的getc(_fp)提出了这样一个场合。
  • 为了安全起见,也许我们可以使用fgetc而不是getc或手动取消宏getc
  • getcIO_getc的弱别名,当宏getc不被使用时生效后,函数的别名将起作用(例如,在c++中隐式地)。

注释清楚地表明,在<stdio.h>中,getcgets 可以是(类函数)宏。C标准当然允许这样做。c++ 编译器库代码,无论出于何种原因,不希望将宏暴露给c++程序;它要确保函数被调用。