11

I just discovered that this compiles fine with no error (gcc 5.3):

std::vector<whatever> vec;

for( e: vec )
  // do something

All the compiler does is issue this warning:

warning: range-based for loop without a type-specifier only available with -std=c++1z or -std=gnu++1z

Could someone explain:

  • what that code does (is it only a way to assume auto without typing it, or is there more ?)
  • what c++1z is (I know c++11, c++14, never heard of c++1z...)
4
  • Oh... just checked second point: C++17 (also called C++1z), from en.wikipedia.org/wiki/C%2B%2B17 Commented Jan 9, 2016 at 19:02
  • 5
    This form of the range-based for loop does not appear to be in the current working paper (available on Github), so I wouldn't call this an "upcoming feature in C++17". It was proposed at some point, but it doesn't look like it got accepted. Commented Jan 9, 2016 at 19:08
  • It was proposed, made its way through the working groups, then rejected by the full committee. Commented Jan 9, 2016 at 19:26
  • To clarify: I discovered this accidentally, I forgot auto when typing too quickly... Commented Jan 9, 2016 at 22:08

1 Answer 1

12

The proposal (which hasn't been accepted, so it's not scheduled to become an official part of the language) was that when you omitted the type specifier, that the type specifier would be equivalent to auto &&, so your for loop would be equivalent to:

std::vector<whatever> vec;

for( auto &&e: vec )
  // do something

For further details, such as the motivation and specific effects on the (then current) standard, see the proposal, N3853.

For completeness: C++1z was a code-name for what became C++17. It came about rather accidentally: what became C++ 11 was referred to as "C++0x" for quite a while. When it was getting close to finished, people wanted a way to refer to the next version, so they incremented the x (which had originally just stood for "some unknown digit") to y. After that, obviously enough, came z, giving C++1z.

Sign up to request clarification or add additional context in comments.

5 Comments

I see. Shouldn't this be considered as a bug in gcc, as a decent compiler shoudn't accept a structure that hasn't made its way throught the C++ commitee ?
I suppose technically it's a bug, yes. It's pretty routine for things like this to happen though--it looked like the proposal would be accepted, so the implemented it. Then it was rejected, but it's still there in the implementation. I suppose at some point, they'll update g++ to match (just haven't yet).
GCC removed it from trunk a few months ago. I kinda wish they had repurposed the code to produce an error instead - right now GCC's diagnostic for for(x : v) is terrible.
Makes senses, sure. So not really a bug, more of a decision-making problem. I understand the gcc team wants to keep up with recent advances, but my feeling is that new features not already accepted should trigger an error. Unless, of course, the user passes a flag meaning "ok, I want to use these new features". But I am not involved at all in that process, so maybe I am missing some point.
@kebs It was a weird case. Usually once a feature passes all the relevant working groups, it gets voted on by the full committee at the same meeting, so you pretty much always know for sure whether it's in or not. But this one had a one-meeting delay between passing CWG and full committee vote, due to some arcane ISO rules, and since it was such a simple feature, it was implemented it in the mean time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.