Why does GCC 6.3 compile this Braced-Init-List code without explicit C++11 support?
Asked Answered
S

1

13

I have a question about the different meanings of a curly-brace enclosed list.

I know that C++03 did not support C++11's initializer_list. Yet, even without the -std=c++11 compiler flag, gcc 6.3 will properly initialize interpolate with this code:

map<string, string> interpolate = { { "F", "a && b && c" }, { "H", "p ^ 2 + w" }, { "K", "H > 10 || e < 5" }, { "J", "F && !K" } };

I was challenged on why this would work, and I realized I didn't have an answer. This is a Brace-Init-List, but the way we get from that to initializing a standard container is typically through an initializer_list. So how would non-C++11 code be accomplishing the initialization?

Sprat answered 20/6, 2017 at 13:39 Comment(2)
I am quite surprised that this question made to 'Hot Network Questions' List :| I thought this was well documented by gcc and several places over the internet about default compilation flags in newer gccLewie
@Lewie I'm a little surprised too. At the time I asked it I assumed, that similarly to the default behavior in gcc 5, I was using "-std=c++98". I'm guessing a lot of people just happened to be on who had the same preconception. Don't worry, I'm sure it was just an aberration, the regular C++ down-vote ratio should be restored soon.Sprat
F
32

The default compiler command for gcc 6.x is -std=gnu++14, so the compiler is implicitly compiling your code using a later version of the C++ language standard.

You will need to manually specify -std=c++03 if you want to compile in C++03.

Fritzfritze answered 20/6, 2017 at 13:41 Comment(4)
A more interesting question is why people would expect a new compiler to still default to a 14-year-old Standard! Anyway, the fact that g++ 6 stopped doing that is a great thing, but I wish the default was c++14, without any extensions; otherwise, people might assume that what g++ allows by default is guaranteed to be well-defined, which is not the case.Fechner
It isn't as unreasonable of an assumption as you make it out to be, @underscore_d. There are two basic strategies for choosing defaults: (A) make them what you think is most frequently desired, or (B) make them as fail-safe as possible. If strategy (B) was used, then defaulting to C++03 would make good sense. This also facilitates upgrading existing code bases to a later version of the compiler, which strategy (A) would seriously complicate, assuming as it does that all code being compiled with that compiler is new code. Agreed completely on not defaulting to Gnu extensions, though!Watkins
@CodyGray Right, good points. Plus, the expectation that the default standard will be... vintage has a precedent: g++ 5 still defaulted to C++98, fer goodness' sakes!Fechner
@CodyGray Thanks for the improvement. Yes, I was trying to be cleaver and play on out-smarted.Fritzfritze

© 2022 - 2024 — McMap. All rights reserved.