C++:将 __PRETTY_FUNCTION__ 附加到 c 字符串

标签 c++ gcc c-preprocessor

我试图在编译时将 __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++:将 __PRETTY_FUNCTION__ 附加到 c 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51747622/

相关文章:

c - 宏预处理器是在什么情况下自行实现的?

c++ - 在编译时验证对象是否创建为 shared_ptr

c++ - 如果我的 CPU 负载表明不是这样,我应该启动多个线程吗?

c - 在 ARM Cortex M0 uart 控制台使用 printf() 打印 float

c - Linux C : error of redefinition of in include headers

c - #ifdef 里面#define

c++ - 查找两个数之间的质数

c++ - 究竟标准的哪些部分需要涉及运算符 < 的重大更改?

无法在 malloc() 处设置断点

c++ - Visual C++ 2010 中的预处理器指令缩进