#ifdef 在同一个头文件中返回不同的值
#ifdef is returning different values inside the same header file
我有两个 C++ 文件 main.cpp
和 client.cpp
,以及一个头文件 action.h
。我正在尝试使用预处理器指令模拟一个简单的客户端-服务器场景。代码如下:
//main.cpp
#include "action.h"
extern void connect();
static Action server;
int main()
{
server.init();
connect();
server.execute();
return 0;
}
//client.cpp
#define CLIENT
#include "action.h"
void connect()
{
Action client;
client.init();
client.execute();
}
和头文件。
#include <iostream>
struct Action
{
#ifdef CLIENT
int data = 10;
#else
int data = 13;
#endif
inline void init()
{
#ifdef CLIENT
std::cout << "Client " << data << std::endl;
data = -1;
#else
std::cout << "Server " << data << std::endl;
data = 0;
#endif
}
inline void execute()
{
#ifdef CLIENT
std::cout << "Server is " << data << std::endl;
#else
std::cout << "Number of clients is " << data << std::endl;
#endif
}
};
当我运行这个时,我得到以下结果:
Server 13
Server 10
Number of clients is 0
Number of clients is 0
现在我的问题是为什么 #ifdef CLIENT
块在 action.h
(int data = 10)
的声明部分中执行,而 #else
块在 [=20= 中执行] 和 execute()
功能,当我从 client.cpp
文件中调用它们时?为什么不是所有三个调用都转到 #ifdef
部分?
此程序表现出未定义的行为。它违反了 One Definition Rule (ODR)。具体这部分:
[basic.def.odr]/6 There can be more than one definition of a class type, ... inline function with external linkage ... in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D
defined in more than one translation unit, then
(6.1) — each definition of D
shall consist of the same sequence of tokens...
...
If the definitions of D
do not satisfy these requirements, then the behavior is undefined.
在你的例子中,你有两个 class Action
的定义以及它的内联成员函数 Action::init
和 Action::execute
,它们由不同的标记序列组成,多亏了宏技巧。
我有两个 C++ 文件 main.cpp
和 client.cpp
,以及一个头文件 action.h
。我正在尝试使用预处理器指令模拟一个简单的客户端-服务器场景。代码如下:
//main.cpp
#include "action.h"
extern void connect();
static Action server;
int main()
{
server.init();
connect();
server.execute();
return 0;
}
//client.cpp
#define CLIENT
#include "action.h"
void connect()
{
Action client;
client.init();
client.execute();
}
和头文件。
#include <iostream>
struct Action
{
#ifdef CLIENT
int data = 10;
#else
int data = 13;
#endif
inline void init()
{
#ifdef CLIENT
std::cout << "Client " << data << std::endl;
data = -1;
#else
std::cout << "Server " << data << std::endl;
data = 0;
#endif
}
inline void execute()
{
#ifdef CLIENT
std::cout << "Server is " << data << std::endl;
#else
std::cout << "Number of clients is " << data << std::endl;
#endif
}
};
当我运行这个时,我得到以下结果:
Server 13
Server 10
Number of clients is 0
Number of clients is 0
现在我的问题是为什么 #ifdef CLIENT
块在 action.h
(int data = 10)
的声明部分中执行,而 #else
块在 [=20= 中执行] 和 execute()
功能,当我从 client.cpp
文件中调用它们时?为什么不是所有三个调用都转到 #ifdef
部分?
此程序表现出未定义的行为。它违反了 One Definition Rule (ODR)。具体这部分:
[basic.def.odr]/6 There can be more than one definition of a class type, ... inline function with external linkage ... in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named
D
defined in more than one translation unit, then(6.1) — each definition of
D
shall consist of the same sequence of tokens...
...If the definitions of
D
do not satisfy these requirements, then the behavior is undefined.
在你的例子中,你有两个 class Action
的定义以及它的内联成员函数 Action::init
和 Action::execute
,它们由不同的标记序列组成,多亏了宏技巧。