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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 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 gcc – P0W Jun 20 '17 at 18:10
  • 1
    @P0W 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. – Jonathan Mee Jun 20 '17 at 18:25

1 Answers1

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • 18
    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. – underscore_d Jun 20 '17 at 14:43
  • 3
    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! – Cody Gray - on strike Jun 20 '17 at 14:56
  • 3
    @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! – underscore_d Jun 20 '17 at 14:59
  • @CodyGray Thanks for the improvement. Yes, I was trying to be cleaver and play on out-smarted. – NathanOliver Jun 20 '17 at 15:19