Why do -ansi and -std=c++11 conflict in g++?

一个人想着一个人 提交于 2021-02-04 19:04:42

问题


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!


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/17480543/why-do-ansi-and-std-c11-conflict-in-g

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!