检查C ++中是否存在关键字 [英] Check if Keyword Exists in C++

查看:116
本文介绍了检查C ++中是否存在关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编译一个使用C ++关键字覆盖的C ++程序。

I was compiling a C++ program that makes use of the C++ keyword "override".

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

I compiled it successfully in Visual Studio 2010, but now I need to compile it on g++. Only g++ 4.6 is available (and you need g++ 4.7 to support "override").

真正的解决方案是安装g ++ 4.7(现在发生)让我想到。是否有编译时检查是否支持关键字?

The real solution is to install g++ 4.7 (which is happening now), but it got me thinking. Is there a compile-time check to see if a keyword is supported?

我试过:

#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

不是一个符号。

我想要的东西比简单地检查编译器版本,看看它是否是支持该关键字之一。

I would like something more general than simply checking the compiler version to see if it is one of the ones that supports the keyword. How can this be done, if at all?

推荐答案

我知道没有办法从程序中检查特定的关键字。

I know of no way to check for specific keywords from within the program. The best I can offer is checking special predefined macros for specific compilers / compiler versions or language / language versions.

对于前者,有

#if defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))

对于后者,有

#if __cplusplus >= 201103L

这篇关于检查C ++中是否存在关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆