我正在尝试编译一些代码(使用GCC 4.8.2),并且正在获取error: expression cannot be used as a function
。
这是相关的代码。
debug.h
// A macro for code which is not expected to be reached under valid assumptions
#if !defined(NDEBUG)
#define UNREACHABLE() do { \
ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__); \
assert(false); \
} while(0)
#else
#define UNREACHABLE() ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__)
#endif
someFile.cpp (仅默认行确实相关)
HLSLBlockEncoder::HLSLBlockEncoderStrategy HLSLBlockEncoder::GetStrategyFor(ShShaderOutput outputType)
{
switch (outputType)
{
case SH_HLSL9_OUTPUT: return ENCODE_LOOSE;
case SH_HLSL11_OUTPUT: return ENCODE_PACKED;
default: UNREACHABLE(); return ENCODE_PACKED;
}
}
错误:
/.../debug.h:123:90: error: expression cannot be used as a function
#define UNREACHABLE() ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__)
^
/.../someFile.cpp:217:16: note: in expansion of macro 'UNREACHABLE'
default: UNREACHABLE(); return ENCODE_PACKED;
^
我试图了解为什么会发生错误。在查看this question时,我认为问题可能是由于宏中的
__FUNCTION__
,函数(HLSL ...)被用作变量。但是根据the GCC documentation的说法:“GCC提供了三个魔术变量,它们以字符串的形式保存了当前函数的名称”,所以我不认为这是问题所在。还有其他想法吗?
最佳答案
用我发现的解决方案更新它。
感谢以上那些告诉我进一步研究ERR
的人。事实证明,另一个头文件中ERR
的定义重复,这似乎已导致我的错误。更改debug.h中的ERR
的定义以避免这种冲突,从而解决了我的问题。 :)
关于c++ - 表达式不能用作宏扩展中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33289151/