Using a range based for loop in C++11 with an existing variable, I would expect that variable to be filled with the value of the last iteration after the loop. However, I've gotten different results when I tested it.
Example:
#include <iostream>
#include <vector>
using namespace std;
int main() {
std::vector<int> v;
v.push_back(2);
v.push_back(43);
v.push_back(99);
int last = -50;
for (last : v)
std::cout << ":" << last << "\n";
std::cout << last;
return 0;
}
- MSVC 2013 doesn't seem to support range based for loops without type declaration
-
GCC-5.1 either automatically introduces a new variable or sets it back to the initial value, giving
:2
:43
:99
-50
I guess MSVC is just being MSVC again, but what about GCC here? Why is last
not 99
in the last line?
Given the definition by the standard, I would expect the behaviour I described in the first sentence.
{
auto && __range = range_expression ;
for (auto __begin = begin_expr, __end = end_expr;
__begin != __end; ++__begin) {
range_declaration = *__begin;
loop_statement
}
}
range_declaration
being last
and not int last
, this should modify the existing variable.
Aucun commentaire:
Enregistrer un commentaire