我正在尝试让我的项目在 GCC 下编译(Visual Studio 可以完美地编译它)。
我有一个自定义断言函数,它会抛出一条 wstring 消息。它的一部分是 _ _FUNCTION__ 宏,我使用 MSDN 中的 WIDEN 宏将其“统一编码”
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
它在 MSVC 中编译正常,但在 GCC 中打印如下:
error: ‘L__FUNCTION__’ was not declared in this scope
我能想到的唯一解决方案是在运行时使用 mbstowcs 将 __FUNCTION __ 的内容转换为 wstring,但我想找到一种编译时方法来完成它。
感谢您的帮助。
最佳答案
在 GCC 中 __FUNCTION__
是一个非标准扩展。引用:GCC Online Docs
In GCC 3.3 and earlier, in C only,
__FUNCTION__
and__PRETTY_FUNCTION__
were treated as string literals; they could be used to initialize char arrays, and they could be concatenated with other string literals. GCC 3.4 and later treat them as variables, like__func__
. In C++,__FUNCTION__
and__PRETTY_FUNCTION__
have always been variables.
所以在 __FUNCTION__
前面添加 L 只会将其变成 L__FUNCTION__
,这可能是未定义的。
关于c++ - GCC、Unicode 和 __FUNCTION__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2518969/