C++:将 __PRETTY_FUNCTION__ 附加到 c 字符串
C++: append __PRETTY_FUNCTION__ to c string
我试图在编译时将 __PRETTY_FUNCTION__
附加到 c 字符串前缀。但它无法编译。而如果我用 __FILE__
来做,那么它不会失败。
const char* s1= "Prefix" __FILE__; // works
const char* s2= "Prefix" __PRETTY_FUNCTION__; // compilation error
error: expected ',' or ';' before '__PRETTY_FUNCTION__'
const char* s2 = "Prefix" __PRETTY_FUNCTION__;
^~~~~~~~~~~~~~~~~~~
我正在使用 gcc 和 clang。
我错过了什么吗?
来自 GCC documentation for __PRETTY_FUNCTION__
:
[__func__
and __PRETTY_FUNCTION__
] are variables, not preprocessor macros, and may not be used to initialize char arrays or be concatenated with string literals.
根据 C 标准的 6.10.8.1 Mandatory macros,__FILE__
是一个字符串文字:
__FILE__
The presumed name of the current source file (a character string literal).
可以在 16.8 预定义宏名称 的 the C++ standard.
的第 1 段中找到类似的语言
然而,根据 6.4.2.2 Predefined identifiers, paragraph 1 of the C standard __func__
是一个标识符:
The identifier __func__
shall be implicitly declared by the translator
as if, immediately following the opening brace of each function
definition, the declaration
static const char __func__[] = "function-name";
8.4.1 总则 the C++ standard states 的第 8 段:
The function-local predefined variable __func__
is defined as if a
definition of the form
static const char __func__[] = "function-name";
had been provided, where
function-name
is an implementation-defined string. It is unspecified whether such
a variable has an address distinct from that of any other object in the program.
假设 GCC 扩展 __PRETTY_FUNCTION__
作为标准 __func__
实现,作为实际标识符,它不能与字符串文字连接。
我试图在编译时将 __PRETTY_FUNCTION__
附加到 c 字符串前缀。但它无法编译。而如果我用 __FILE__
来做,那么它不会失败。
const char* s1= "Prefix" __FILE__; // works
const char* s2= "Prefix" __PRETTY_FUNCTION__; // compilation error
error: expected ',' or ';' before '__PRETTY_FUNCTION__'
const char* s2 = "Prefix" __PRETTY_FUNCTION__;
^~~~~~~~~~~~~~~~~~~
我正在使用 gcc 和 clang。 我错过了什么吗?
来自 GCC documentation for __PRETTY_FUNCTION__
:
[
__func__
and__PRETTY_FUNCTION__
] are variables, not preprocessor macros, and may not be used to initialize char arrays or be concatenated with string literals.
根据 C 标准的 6.10.8.1 Mandatory macros,__FILE__
是一个字符串文字:
__FILE__
The presumed name of the current source file (a character string literal).
可以在 16.8 预定义宏名称 的 the C++ standard.
的第 1 段中找到类似的语言然而,根据 6.4.2.2 Predefined identifiers, paragraph 1 of the C standard __func__
是一个标识符:
The identifier
__func__
shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declarationstatic const char __func__[] = "function-name";
8.4.1 总则 the C++ standard states 的第 8 段:
The function-local predefined variable
__func__
is defined as if a definition of the formstatic const char __func__[] = "function-name";
had been provided, where function-name is an implementation-defined string. It is unspecified whether such a variable has an address distinct from that of any other object in the program.
假设 GCC 扩展 __PRETTY_FUNCTION__
作为标准 __func__
实现,作为实际标识符,它不能与字符串文字连接。