C++ 功能测试宏在Visual C+;中无法正常工作+;

C++ 功能测试宏在Visual C+;中无法正常工作+;,c++,visual-studio,visual-c++,macros,C++,Visual Studio,Visual C++,Macros,在Visual Studio中测试功能时,我发现了这种奇怪的行为: #ifndef __cpp_constexpr #error Opposite day! //Compiler error #endif #define test_macro #ifndef test_macro #error But only for feature macros? //No compiler error #endif int main() {} \uuuucpp\uconstexpr是明确定义的,我在

在Visual Studio中测试功能时,我发现了这种奇怪的行为:

#ifndef __cpp_constexpr
#error Opposite day! //Compiler error
#endif

#define test_macro

#ifndef test_macro
#error But only for feature macros? //No compiler error
#endif

int main() {}
\uuuucpp\uconstexpr
是明确定义的,我在实际程序中使用了它。 从我的测试来看,对于功能宏
#ifndef
,其行为类似于
#ifdef
反之亦然

这是Visual Studio的bug,还是我遗漏了什么

在VS2017、Visual C++14或更高版本中编译,启用了C++17标准


请注意,Intellisense正在按预期工作,它只是编译器。

您使用的预定义宏和预处理代码不正确。下一个代码可以帮助:

#if defined(__GNUG__) && (__cplusplus > 201103L)
#  define HAS_CONSTEXPR
#elif defined(_MSC_VER) && (_MSC_FULL_VER >= 190024210)
#  define HAS_CONSTEXPR
#endif // __GNUG__

#ifndef HAS_CONSTEXPR
#  error Opposite day! //Compiler error
#else
#   define test_macro
#   ifndef test_macro // useless by why not ? 
#     error But only for feature macros? //No compiler error
#   endif
#endif
建议使用boost配置库。这是交叉编译器,您可以这样解决:

#ifdef BOOST_NO_CXX11_CONSTEXPR
#   error Opposite day! //Compiler error
#endif

它将对Boost支持的编译器进行工作(根据Boost许可证,您可以检查源代码和复制在项目中)

我可以总是测试VisualC++版本,但这不是跨平台的。我敢肯定,这就是我们引入的原因。我也不能使用boost。@andreasxp功能测试宏很好。但是“很难使用某些东西,你的编译器不支持它”(c)Jeff Alger:)我已经为你做了更新gcc@andreasxp_uuCPlusplus宏在MSVC++中没有正确定义(我不知道为什么),因此您必须使用MSC_FULL\u版本checks@VictorGubin因为从技术上讲,他们还没有“满杯”C++11支持还没有-详细信息请参阅本文:FWIW功能测试宏似乎也没有得到其他编译器的正确支持;例如,虽然GCC可以很好地处理
\uuuuuCPP\uConstExpr
,但它在处理其他问题时失败,例如
\uuuuCPP\uLib\uMake\uUnique
。事实上,它们似乎不可靠,因此并非所有的意图和目的都有效。