Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Since C++11, we can write:

vector<int> v{1, 2, 3, 4};
for (auto x : v)
{
    cout << x << endl;
}

According to Essentials of Modern C++ Style, the following code will soon be also legal in C++:

vector<int> v{1, 2, 3, 4};
for (x : v)
{
    cout << x << endl;
}

Will this feature be available in C++17 or C++20?

share|improve this question
23  
No, it will not. That being said, in my opinion, this is not simpler, but rather more confusing. – DeiDei 10 hours ago
1  
@DeiDei Citation needed. So far, it's just your word against Herb Sutter's. – hvd 10 hours ago
2  
@DeiDei, ideone.com/KONqTW – Qwertiy 10 hours ago
6  
@hvd it is the word of someone in 2016 against the word of someone in 2014... – Marc Glisse 10 hours ago
4  
@Qwertiy Okay, after some more research... The compiler used by Ideone is GCC 5.1 which allows this syntax with -std=c++1z. This is not allowed anymore since GCC 6.1. – DeiDei 9 hours ago
up vote 34 down vote accepted

No. This was killed by the committee more than two years ago, mostly because of concerns about potential confusion caused by shadowing:

std::vector<int> v = { 1, 2, 3, 4 };
int x = 0; 
for(x : v) {} // this declares a new x, and doesn't use x from the line above
assert(x == 0); // holds

The objections came up so late in the process that both Clang and GCC had already implemented the feature by the time it got rejected by the full committee. The implementations were eventually backed out: Clang GCC

share|improve this answer
3  
Eh? cplusplus.github.io/EWG/ewg-active.html#81 is still open and says "Author is going to revise". – hvd 9 hours ago
2  
@hvd The EWG issues list hasn't been updated in more than a year. IIRC STL is not actively working on any revision. In any event, it seems clear that the committee isn't going to accept anything that doesn't somehow resemble a declaration at the minimum. – T.C. 9 hours ago
3  
Oh... that's really unfortunate regardless of the merits of the proposal. It's important to keep such lists up to date. – hvd 9 hours ago
    
By the way, is such code valid and what it should output? – Qwertiy 9 hours ago
1  
@Qwertiy That's a wholly different question, but no, that's not valid. The syntax for the range-based for requires a declaration, not merely an expression. – hvd 9 hours ago

This is still an open issue. There was a proposal, linked there, to add this to C++17. That proposal was rejected. Whether a new proposal will be accepted depends on the proposal, so it's too soon to say whether C++20 might have it.

share|improve this answer

Update

GCC 5.1 allows this syntax with -std=c++1z.
This is not allowed anymore since GCC 6.1.

So this answer doesn't seem to be correct.

Ideone compiler successfully compiles such code under C++ 14:

http://ideone.com/KONqTW

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> v {1, 2, 3, 4};

    for (x : v)
        cout << x << endl;

    return 0;
}
share|improve this answer
1  
That doesn't sound like it is available, but that it was available for a while, but no longer. – CodesInChaos 8 hours ago
1  
@CodesInChaos, updated the answer. – Qwertiy 8 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.