c++ - 使用宏替代功能

标签 c++ c c-libraries

我正在阅读C库中 header 的实现,在那里遇到了函数的宏重写以及函数声明。我想知道这有什么用,即要么应使用宏,要么应使用函数,则需要重写什么?

编辑:
例:

/* ctype.h standard header */
#ifndef _CTYPE
#define _CTYPE
/* Ctype code b i t s */
#define 0x200 /* extra alphabetic */
#define _XS 0x100 /* extra space */
#define _BB 0x80 /* BEL, BS, etc. */
#define _CN 0x40 /*CR, FF, HT, NL, V T */
#define _DI 0x20 /* '0'_' 9' */
#define _LO 0x10 /* 'a'_'2'*/
#define _PU 0x08 /* punctuation */
#define _SP 0x04 /* space */
#define _UP 0x02 /* 'A' _ ' Z ' */
#define _XD 0x01 /* 'Or_'9', 'A'_'Fr, ' a r _ ' f r * /
/* ********declarations********** */
int isalnum(int) , isalpha (int) , iscntrl (iny) , isdigit (int) ;
int isgraph (int) , islower (int) , isprint (int) , ispunct (int) ;
int isspace (int) , isupper (int) , isxdigit (int) ;
int tolower (int) , toupper (int) ;
extern const short *_Ctype, *_Tolower, *_Toupper;

/************ macro overrides*********** */
#define isalnum(c) (_Ctype [ (int)(C) ] & (_DI | _LO | _UP | _XA) )
#define isalpha (c) (_Ctype [ (int)(C) ] & (_LO | _UP | _XA) )
#define i s c n t r l (c) (_Ctype [ (int)(C) ] & (_BB | _CN) )
#define isdigit (c) (_Ctype [ (int)(C) ] & _DI)
#define isgraph (c) (_Ctype [ (int)(C) ] & (_DI | _LO| _PU| _UP | _XA) )
#define islower (c) (_Ctype [ (int)(C) ] & _LO)
#define isprint (c) \
(_Ctype[(int) (c)1 & (_DI| _LO| _PU| _SP| _UP| _XA))
#define ispunct (c) (_Ctype [ ( int ) (c) ] & _PU)
#define isspace (c) (_Ctype [ ( int ) (c) ] & (_CN | _SP | _XS) )
#define isupper (c) (_Ctype [ ( int ) (c) ] & _UP)
#define isxdigit (c) (_Ctype [ ( int ) (c) ] & _XD)
#define tolower (c) _Tolower [ ( int ) (c) ]
#define toupper (c) _Toupper [ ( int ) (c) ]
#endif

*更不用说功能定义在单独的文件中

最佳答案

C标准要求必须将其指定的函数定义为函数,并且必须在适当的 header 中声明它们,以便可以将指针传递给周围的函数。
C标准允许函数被类似函数的宏覆盖。
该标准有一组坚固的规则,我已将其重新格式化为项目符号列表。前两个要点与问题没有直接关系。

§7.1.4 Use of library functions

¶1 Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow:

  • If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined.
  • If a function argument is described as being an array, the pointer actually passed to the function shall have a value such that all address computations and accesses to objects (that would be valid if the pointer did point to the first element of such an array) are in fact valid.
  • Any function declared in a header may be additionally implemented as a function-like macro defined in the header, so if a library function is declared explicitly when its header is included, one of the techniques shown below can be used to ensure the declaration is not affected by such a macro.
  • Any macro definition of a function can be suppressed locally by enclosing the name of the function in parentheses, because the name is then not followed by the left parenthesis that indicates expansion of a macro function name. For the same syntactic reason, it is permitted to take the address of a library function even if it is also defined as a macro.185)
  • The use of #undef to remove any macro definition will also ensure that an actual function is referred to.
  • Any invocation of a library function that is implemented as a macro shall expand to code that evaluates each of its arguments exactly once, fully protected by parentheses where necessary, so it is generally safe to use arbitrary expressions as arguments.186)
  • Likewise, those function-like macros described in the following subclauses may be invoked in an expression anywhere a function with a compatible return type could be called.187)
  • All object-like macros listed as expanding to integer constant expressions shall additionally be suitable for use in #if preprocessing directives.

¶2 Provided that a library function can be declared without reference to any type defined in a header, it is also permissible to declare the function and use it without including its associated header.



185) This means that an implementation shall provide an actual function for each library function, even if it also provides a macro for that function.

186) Such macros might not contain the sequence points that the corresponding function calls do.

187) Because external identifiers and some macro names beginning with an underscore are reserved, implementations may provide special semantics for such names. For example, the identifier _BUILTIN_abs could be used to indicate generation of in-line code for the abs function. Thus, the appropriate header could specify

     #define abs(x) _BUILTIN_abs(x)

for a compiler whose code generator will accept it. In this manner, a user desiring to guarantee that a given library function such as abs will be a genuine function may write

     #undef abs

whether the implementation's header provides a macro implementation of abs or a built-in implementation. The prototype for the function, which precedes and is hidden by any macro definition, is thereby revealed also.



问题中的标题说明了保留标识符的使用(第7.1.3节“保留标识符”(http://port70.net/~nsz/c/c11/n1570.html#7.1.3))。它声明指定<ctype.h> header 声明的函数。它提供了重写这些功能的宏,因为相信使用这些功能比调用实现数组访问的功能要快。
这样实现后,如果需要将指向分类或转换函数之一的指针传递给其他代码,则可以这样做。如果仅提供了宏,则必须拉一些特技才能使实际函数作为指针传递。
该标准仔细规定了一些宏实际上必须是宏-offsetof()va_start()以及va_arg()是想到的三个。但是标准中的绝大多数功能都必须实现为功能-但如果实现者认为合适的话,则可以被宏覆盖。
宏必须是类似于函数的宏的要求也很重要。它允许使用名称,而不必在括号后加上指向该函数的指针。如果宏不是函数式的(如果 header 包含类似#define isupper _IsUpper而不是#define isupper(c) _IsUpper(c)的东西),那么就不可能依靠访问标准函数名-而¶2规则允许您编写代码(不包括<ctype.h> ):
extern int isupper(int c);
并且您将保证在库中有一个符合期望的isupper()函数(即使也有一个_IsUpper()函数)。

关于c++ - 使用宏替代功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61038709/

相关文章:

vsnprintf 能否返回大于 1 的负值?

c++ - 如何在 C++ 中打印堆栈

c++ - sigma_s和sigma_r的详细含义OpenCV上的Enhance函数

c# - 如何从 Xamarin Forms 项目调用 C++ 库代码 - DllNotFoundException

c - 如何在 C 中指向不兼容的指针类型?

c - C 的变体数据类型库

c++ - 线程权限

c++ - C 代码的意外输出

c - 如何计算基于 linux 的 pc 上的接收数据包速率?如 pps 或 fps

c - 如何使用prolog调用酷图库函数?