Search code examples
c++c++11gccg++clang

warning: 'auto' type specifier is a C++11 extension


I have a very simple C++ code statement auto a = 12;.

When I am compiling it with g++ in Linux using the -std=c++98 option I am getting an error as expected:

error: ‘a’ does not name a type

But when I am compiling the same code with the same option in MacOS I am getting just a warning, but the code gets compiled fine:

warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]

But wasn't the whole point of -std=c++98 to compile the code following C++ 98 standards? So the warning says that even though auto is a C++11 extension I am going to compile it for you?

Is there any option to force using c++98 (or other standard)?

g++ --version prints Apple clang version 12.0.0 (clang-1200.0.32.29) which is another weird thing by the way. So it is clang actually.

It's like asking firefox --version and getting chrome 87.0.4280.163


Solution

  • On MacOS, you're using clang, not gcc. (If I recall correctly, MacOS provides "gcc" and "g++" as symlinks to "clang" and "clang++", respectively, so that scripts that assume gcc don't break.)

    The two compilers just treat this case differently.

    Yes, a compiler that conforms to the 1998 ISO C standard, as both gcc and clang attempt to do with -std=c++98, must diagnose that line.

    As far as the standard is concerned, a non-fatal warning is a valid diagnostic. The standard doesn't require an invalid program to be rejected (unless it contains a #error directive).

    If you want to strictly enforce C++98 rules and reject code that violates them, use -std=c++98 -pedantic-errors (with either gcc or clang).