4

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

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").

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?

I tried:

#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

This doesn't work, since "override" is not a symbol.

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?

geometrian
  • 14,775
  • 10
  • 56
  • 132
  • You could change "#ifndef override" for checking not symbol but compiler version as a partial solution – Arsenii Fomin Jan 14 '13 at 06:15
  • 1
    Try [GNU C++ how to check when -std=c++0x is in effect?](http://stackoverflow.com/q/2958398) and [How do I check for C++11 support?](http://stackoverflow.com/q/5047971) and [Is there a preprocessor directive for detecting C++11x support?](http://stackoverflow.com/q/10717502) – Karthik T Jan 14 '13 at 06:16
  • possible duplicate of [Is there a preprocessor directive for detecting C++11x support?](http://stackoverflow.com/questions/10717502/is-there-a-preprocessor-directive-for-detecting-c11x-support) – Karthik T Jan 14 '13 at 06:17
  • 2
    Note that `override` is not a keyword in C++11. It is a *special identifier*, which only has meaning in a certain context. – juanchopanza Jan 14 '13 at 06:22
  • @juanchopanza: I didn't know that, but I found the relevant resource here: http://en.cppreference.com/w/cpp/keyword. Thanks! – geometrian Jan 14 '13 at 07:35
  • This seems like a solution in search of a problem to me. Just rename the function, or use initial caps it the name must be 'OverRide' – Jeff D. Jan 14 '13 at 06:54

1 Answers1

5

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.

For the former, there is

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

For the latter, there is

#if __cplusplus >= 201103L
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • I am surprised the commitee did not define specific macros for feature support. – Martin York Jan 14 '13 at 06:36
  • Well, it's not the answer I wanted, but after some more looking around at some surprisingly similar problems that my searches somehow didn't turn up, I have to conclude that that this is the _right_ answer. Thanks! – geometrian Jan 14 '13 at 07:36
  • @LokiAstari: There's a good reason for that. ISO SQL had such a concept, but that didn't work out at all. No two implementations were compatible, even though they all correctly specified which parts were implemented. But each implemented a different subset, and the common subset was almost empty. – MSalters Jan 14 '13 at 07:41