例如,在
include/cpprest/http_msg.h
中,header_names
定义为:
class header_names
{
public:
#define _HEADER_NAMES
#define DAT(a, b) _ASYNCRTIMP const static utility::string_t a;
#include "cpprest/details/http_constants.dat"
#undef _HEADER_NAMES
#undef DAT
};
并且在
cpprest/details/http_constants.dat
中会有这样的部分:
#ifdef _HEADER_NAMES
DAT(accept, "Accept")
DAT(accept_charset, "Accept-Charset")
...
#endif
根据
DAT(a, b)
的定义,未使用变量 b
。
但是当我们做一个简单的作业时:
auto a = header_names::accept;
变量
a
包含字符串 "Accept"
。
我不知道怎么会发生这种事。
我使用
godbolt.org
和选项 -E
来获取以下代码的预处理器
class header_names
{
public:
#define DAT(a, b) _ASYNCRTIMP const static utility::string_t a;
DAT(accept, "Accept")
DAT(accept_charset, "Accept-Charset")
#undef DAT
};
生成的结果是
class header_names
{
_ASYNCRTIMP const static utility::string_t accept;
_ASYNCRTIMP const static utility::string_t accept_charset;
};
release/src/http/common/http_msg.cpp
。
#define _HEADER_NAMES
#define DAT(a, b) const utility::string_t header_names::a = _XPLATSTR(b);
#include "cpprest/details/http_constants.dat"
#undef _HEADER_NAMES
#undef DAT