c++ - 为什么不能将字符串文字连接到 __FUNCTION__?

标签 c++ macros c-preprocessor

__FUNCTION__ 不是字符串文字吗?我一直认为它是 - 沿着 __FILE__ 的路线,但我刚刚发现我不能将字符串文字相邻地连接到它。如果它不是字符串文字,它被定义为什么?我无法让 cscope 解决它。

例如

#include <iostream>

int main( int argc, char* argv[] )
{
  std::cout << __FILE__ << std::endl;
  std::cout << __FILE__ "A" << std::endl;
  std::cout << __FUNCTION__ << std::endl;
  //std::cout << __FUNCTION__ "A" << std::endl; // Doesn't compile.
  return 0;
}

包含问题行时的错误:

>g++ --version
g++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

>g++ -g main.cpp 
main.cpp: In function 'int main(int, char**)':
main.cpp:8:29: error: expected ';' before string constant
   std::cout << __FUNCTION__ "A" << std::endl; // Doesn't compile.

最佳答案

Isn't __FUNCTION__ a string literal?

没有。

来自 https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Function-Names.html

These identifiers are variables, not preprocessor macros, and may not be used to initialize char arrays or be concatenated with string literals.

关于c++ - 为什么不能将字符串文字连接到 __FUNCTION__?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48124994/

相关文章:

c++ - 前缀运算符的实现

c++ - 错误:没有用于调用 Counter::Counter() 的匹配函数

c++ - 如何使用 Cython 保持 C++ 类名不被修改?

rust - 如何在程序宏生成的代码中创建卫生标识符?

c++ - 如何查看C++宏的内容?

c - 将 pragma 扩展为注释(对于 doxygen)

c - 预处理器指令中令人困惑的术语

c++ - 如何使用模板初始化类中的静态成员数组

c++ - 如何使用宏动态加载 dll

ios - 尝试将 #define DEBUG_MODE 添加到我的 prefix.pch 文件时出现编译器警告