检查c++中是否存在Keyword

Check if Keyword Exists in C++

本文关键字:存在 Keyword 是否 c++ 检查      更新时间:2023-10-16

我正在编译一个使用c++关键字"override"的c++程序。

我在Visual Studio 2010中成功编译了它,但现在我需要在g++上编译它。只有g++ 4.6可用(你需要g++ 4.7来支持"override")。

真正的解决方案是安装g++ 4.7(现在正在发生),但它让我思考。是否有编译时检查以查看是否支持关键字?

我试着:

#ifndef override
    #define override
    #ifdef BUILD_WINDOWS
        #pragma message("The "override" keyword is not supported on this compiler!  Ignoring it!")
    #else
        #warning "The "override" keyword is not supported on this compiler!  Ignoring it!"
    #endif
#endif

这行不通,因为"override"不是一个符号。

我想要一些更一般的东西,而不是简单地检查编译器版本,看看它是否是支持关键字的一个。如果有的话,该如何做到这一点呢?

我知道没有办法从程序中检查特定的关键字。我能提供的最好的是检查特定编译器/编译器版本或语言/语言版本的特殊预定义宏。

对于前者,有

#if defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
对于后者,有
#if __cplusplus >= 201103L