6

Why does -ansi and -std=c++11 doesn't work together ? (ANSI reverts back to C++98 according to other answers) I am using g++-4.8.

Here is the ANSI ratification of C++11 :

http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2fISO%2fIEC+14882-2012

This leaves me perplex. Thanks!

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Jean-Michaël Celerier
  • 7,412
  • 3
  • 54
  • 75

3 Answers3

12

RTFM:

-ansi
In C mode, this is equivalent to -std=c89. In C++ mode, it is equivalent to -std=c++98.

The flag was added before there were multiple versions of the standard, and the flag still has the same meaning.

Why do you want to use both anyway? If you want -std=c++11 then use that, don't add another flag that means something else contradictory.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • 2
    I didn't really want to use both, but I modified code that was having -ansi flag in the makefile by adding some C++11 goodness, and at first I only did add -std=c++11 to the flags, without knowing that it would be countered by -ansi. – Jean-Michaël Celerier Jul 11 '13 at 06:23
3

They are just flag names. It's better to avoid calling anything just "ANSI" because 1. they standardize so many things, 2. old standards are updated regularly with meaningful changes, and 3. they don't have particular involvement in any computer languages I'm aware of, but rather participate as a member of some international organization, in the case of C++, ISO.

When telling a compiler what standardized language dialect you want, specify the year of its standardization, or the only assumption it can make is that you want the first (oldest) standard. The initial standardization is the only point in time when failing to specify the year doesn't result in ambiguity.

Speaking of ambiguity, according to the documentation, -ansi is the only language flag whose meaning depends on the invocation of GCC. gcc -ansi means C89, and g++ -ansi means C++03. Personally I prefer less ambiguity over more cleverness.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
2

Yes. It's the same story in C as in C++: ANSI has adopted (almost automatically) every version of the ISO C standard as it came out, including C11: http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2fISO%2fIEC+9899-2012 . Each new standard "cancels and replaces" the earlier standard, so as you note, the official ANSI standards are C11 and C++11, and the earlier versions are no longer valid law. The ANSI standard is the current ISO standard, period.

So if you want to read the --ansi flag as indicating the standard currently adopted by ANSI, it would be entirely unnecessary.

Instead, the term "ANSI" in the C and C++ context is used to refer to the first standardized version of the language, before later features were added. This is just a custom, and like all customs, doesn't have to make literal sense. It is convenient because some people (typically casual C or C++ users) are not as well-read about what standards bodies do, and believe that ANSI's last C standard really is the one from 1989. Those people will rely on the --ansi flag, while the rest of us use the correct year-labeled standard.

bk.
  • 6,068
  • 2
  • 24
  • 28