# 运算符在 C++ 宏中做什么?
What does # operator do in C++ macros?
我遇到了这个宏:
#define STR_ERROR(ecode) case ecode: return #ecode;
#ecode
部分有什么作用?
ecode 是一个 int,而这个函数 returns 是一个 const char*。
我确定已经回答了这个问题,但我的 search-foo 放弃了我。 ecode
本身特定于此代码。搜索 c++ #
可提供有关宏的一般信息(以及一些与 C++ 相关的编号列表)。
根据cppreference:
# operator before an identifier in the replacement-list runs the identifier through parameter replacement and encloses the result in quotes, effectively creating a string literal
示例来自 Microsoft Docs
#include <stdio.h>
#define stringer( x ) printf_s( #x "\n" )
int main() {
stringer( In quotes in the printf function call );
stringer( "In quotes when printed to the screen" );
stringer( "This: \" prints an escaped double quote" );
}
结果:
In quotes in the printf function call
"In quotes when printed to the screen"
"This: \" prints an escaped double quote"
Google-Fu protip: 我刚刚搜索了C++ macro #
,Google建议在最后加上operator
,这些文档位于第一页。
我遇到了这个宏:
#define STR_ERROR(ecode) case ecode: return #ecode;
#ecode
部分有什么作用?
ecode 是一个 int,而这个函数 returns 是一个 const char*。
我确定已经回答了这个问题,但我的 search-foo 放弃了我。 ecode
本身特定于此代码。搜索 c++ #
可提供有关宏的一般信息(以及一些与 C++ 相关的编号列表)。
根据cppreference:
# operator before an identifier in the replacement-list runs the identifier through parameter replacement and encloses the result in quotes, effectively creating a string literal
示例来自 Microsoft Docs
#include <stdio.h>
#define stringer( x ) printf_s( #x "\n" )
int main() {
stringer( In quotes in the printf function call );
stringer( "In quotes when printed to the screen" );
stringer( "This: \" prints an escaped double quote" );
}
结果:
In quotes in the printf function call
"In quotes when printed to the screen"
"This: \" prints an escaped double quote"
Google-Fu protip: 我刚刚搜索了C++ macro #
,Google建议在最后加上operator
,这些文档位于第一页。