Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 274
Ashot Khachatryan Avatar asked Sep 06 '25 03:09

Ashot Khachatryan


2 Answers

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

like image 70
Keith Thompson Avatar answered Sep 11 '25 17:09

Keith Thompson


You may be looking for -pedantic or -pedantic-errors, quoting man g++:

-pedantic

Issue all the warnings demanded by strict ISO C and ISO C ++ ; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C ++ . For ISO C, follows the version of the ISO C standard specified by any -std option used.

Note: this is not meant as a feature to check strict standards conformance (see the rest of the description in the man page)


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!