5

The directive:

#ifndef __cplusplus
  #error C++ is required
#elif __cplusplus < 201402L
  #error C++14 is required
#endif

The command-line: g++ -Wall -Wextra -std=c++14 -c -o header.o header.hpp

My g++ version: g++ (tdm-1) 4.9.2

The error C++14 is required is generated even when I added -std=c++14, I don't know why.

Please tell me how to fix this.

manlio
  • 18,345
  • 14
  • 76
  • 126
DMaster
  • 603
  • 1
  • 10
  • 23
  • 4
    Some compilers would treat `-std=c++14` as a request to enable whatever C++14 features were implemented, while `201402L` would imply *complete* support for that standard, so it's not necessarily simply an error/bug with GCC. Go read the GCC docs if you want to understand what it's doing.... – Tony Delroy Jun 23 '15 at 06:55

1 Answers1

14

According to the GCC CPP manual (version 4.9.2 and 5.1.0):

__cplusplus This macro is defined when the C++ compiler is in use. You can use __cplusplus to test whether a header is compiled by a C compiler or a C++ compiler. This macro is similar to __STDC_VERSION__, in that it expands to a version number. Depending on the language standard selected, the value of the macro is 199711L, as mandated by the 1998 C++ standard; 201103L, per the 2011 C++ standard; an unspecified value strictly larger than 201103L for the experimental languages enabled by -std=c++1y and -std=gnu++1y.

You can check that g++ --std=c++14 defines __cplusplus as:

 Version    __cplusplus
  4.8.3       201300L
  4.9.2       201300L
  5.1.0       201402L

For clang++ --std=c++14:

 Version    __cplusplus
  3.3          201305L
  3.4          201305L
  3.5.x        201402L
  3.6          201402L
  3.7          201402L

So a safer check should probably be:

#ifndef __cplusplus
#  error C++ is required
#elif __cplusplus <= 201103L
#  error C++14 is required
#endif

As specified in the comment, this could mean partial C++14 support.

To check for a specific feature you could also try Boost Config (especially Macros that describe C++14 features not supported).

manlio
  • 18,345
  • 14
  • 76
  • 126
  • 1
    Does the `__cplusplus` macro gets set when the extension of the file is `.cpp` or when the program is compiled using a compiler. Because when I wrote a simple program using the above information and compiled the program using `gcc` rather than `g++`, the compiler aborts the compilation but on the basis of `#elif` and if I don't write the `#elif` condition and compile the program using `gcc` it doesn't abort according to the condition `#ifndef __cplusplus` Can you clarify about what is actually happening? Code Reference: https://pastebin.com/iiuzPrwW – strikersps Apr 21 '20 at 07:28
  • 2
    @strikersps The `__cplusplus` preprocessor macro is defined if the compilation unit is compiled with a C++ compiler. Technically, either `gcc` or `g++` can be used for general C++ development with applicable option settings. However, the `g++` default behavior is naturally aligned to a C++ development. See https://stackoverflow.com/q/172587/3235496 for further details. – manlio Apr 21 '20 at 08:21