将 C 宏定义为自身的目的是什么? (见 glibc 头文件)

问题描述 投票:0回答:1

在 Ubuntu Linux 20.04 机器上,在

/usr/include/x86_64-linux-gnu/bits/resource.h
中,我看到如下代码:

enum __rusage_who
{
  /* The calling process.  */
  RUSAGE_SELF = 0,
#define RUSAGE_SELF RUSAGE_SELF

  /* All of its terminated child processes.  */
  RUSAGE_CHILDREN = -1
#define RUSAGE_CHILDREN RUSAGE_CHILDREN
};

我们看到

RUSAGE_SELF
被定义为它本身。其目的/好处是什么?

c c-preprocessor
1个回答
0
投票

同一文件的评论中对此进行了解释。

/* Transmute defines to enumerations.  The macro re-definitions are
necessary because some programs want to test for operating system
features with #ifdef RUSAGE_SELF.  In ISO C the reflexive
definition is a no-op.  */

所以它们只是为了功能测试而工作。

    

© www.soinside.com 2019 - 2024. All rights reserved.