为什么#define和typedef操作数被反转?

fou*_*nes 5 c typedef c-preprocessor

以下定义A替换B:

#define A B
Run Code Online (Sandbox Code Playgroud)

而这定义A为该类型 的别名B:

typedef B A;
Run Code Online (Sandbox Code Playgroud)

为什么?这不连贯吗?

In *_*ico 7

简单地说:考虑以下变量声明:

// declare a variable called "myInt" with type int
int myInt;
// declare a variable called "myDouble" with type double
double myDouble;
// declare a variable called "myLong" with type long
long myLong;
// declare a variable called "myFunc" with type pointer to function
void (*myFunc)(char*);
Run Code Online (Sandbox Code Playgroud)

那么typedefs很有意义:

// declare a type alias called "myInt" with type int
typedef int myInt;
// declare a type alias called "myDouble" with type double
typedef double myDouble;
// declare a type alias called "myLong" with type long
typedef long myLong;
// declare a type alias called "myFunc" with type pointer to function
typedef void (*myFunc)(char*);
Run Code Online (Sandbox Code Playgroud)

另一方面,宏可以采用函数式语法:

#define A(B, C) B, C

A(1, 2) // expands out to 1, 2
Run Code Online (Sandbox Code Playgroud)

因此对于宏来说,"名称"之后的"定义"更有意义.

(顺便说一下,这也适用于C++.)