Skip to content

Commit 7e635c2

Browse files
hsutterzaucy
authored andcommitted
Correct the range-for implementation to just declare const& instead of using as_const, closes hsutter#234
1 parent 30ea232 commit 7e635c2

10 files changed

+20
-21
lines changed

regression-tests/test-results/mixed-bounds-safety-with-assert-2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ auto add_42_to_subrange(auto& rng, cpp2::in<int> start, cpp2::in<int> end) -> vo
2020
std::vector<int> v {1, 2, 3, 4, 5};
2121
add_42_to_subrange(v, 1, 3);
2222

23-
for ( auto&& cpp2_range = std::as_const(v); auto const& i : cpp2_range )
23+
for ( auto const& cpp2_range = v; auto const& i : cpp2_range )
2424
std::cout << i << "\n";
2525
}
2626

regression-tests/test-results/mixed-bounds-safety-with-assert.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ auto print_subrange(auto const& rng, cpp2::in<int> start, cpp2::in<int> end) ->
2626
cpp2::Bounds.expects(cpp2::cmp_less_eq(end,CPP2_UFCS_0(ssize, rng)), "");
2727

2828
auto count {0};
29-
for ( auto&& cpp2_range = std::as_const(rng);
29+
for ( auto const& cpp2_range = rng;
3030

3131
auto const& i : cpp2_range ) { do
3232
if (cpp2::cmp_less_eq(start,count) && cpp2::cmp_less_eq(count,end)) {

regression-tests/test-results/mixed-function-expression-and-std-for-each.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
std::move(callback)
3535
);
3636

37-
for ( auto&& cpp2_range = std::as_const(view); auto const& str : cpp2_range ) {
37+
for ( auto const& cpp2_range = view; auto const& str : cpp2_range ) {
3838
std::cout << str << "\n";
3939
}
4040
}

regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
auto callback {[](auto& x) { return x += "-ish"; }};
2828
std::ranges::for_each( view, std::move(callback));
2929

30-
for ( auto&& cpp2_range = std::as_const(view); auto const& str : cpp2_range )
30+
for ( auto const& cpp2_range = view; auto const& str : cpp2_range )
3131
std::cout << str << "\n";
3232
}
3333

regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
auto callback {[](auto& x) { return x += "-ish"; }};
2727
std::ranges::for_each( view, std::move(callback));
2828

29-
for ( auto&& cpp2_range = std::as_const(view); auto const& str : cpp2_range )
29+
for ( auto const& cpp2_range = view; auto const& str : cpp2_range )
3030
std::cout << str << "\n";
3131
}
3232

regression-tests/test-results/mixed-function-expression-with-pointer-capture.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
auto callback {[](auto& x) { return x += "-ish"; }};
2929
std::ranges::for_each( view, std::move(callback));
3030

31-
for ( auto&& cpp2_range = std::as_const(view); auto const& str : cpp2_range )
31+
for ( auto const& cpp2_range = view; auto const& str : cpp2_range )
3232
std::cout << str << "\n";
3333
}
3434

regression-tests/test-results/mixed-function-expression-with-repeated-capture.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
auto callback {[](auto& x) { return x += "-ish"; }};
2828
std::ranges::for_each( view, std::move(callback));
2929

30-
for ( auto&& cpp2_range = std::as_const(view); auto const& str : cpp2_range )
30+
for ( auto const& cpp2_range = view; auto const& str : cpp2_range )
3131
std::cout << str << "\n";
3232
}
3333

regression-tests/test-results/mixed-intro-for-with-counter-include-last.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{
1919
std::vector<int> v {1, 2, 3, 4, 5};
2020
auto counter {42};
21-
for ( auto&& cpp2_range = std::as_const(v); auto const& i : cpp2_range ) { do {
21+
for ( auto const& cpp2_range = v; auto const& i : cpp2_range ) { do {
2222
std::cout << i << " " << counter << "\n";
2323
} while (false); counter *= 2; }
2424
}

regression-tests/test-results/pure2-break-continue.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ auto for_continue_inner() -> void
196196
{
197197
std::vector vi {0, 1, 2};
198198
auto counter {0};
199-
for ( auto&& cpp2_range = std::as_const(vi); auto const& i : cpp2_range ) { do {
199+
for ( auto const& cpp2_range = vi; auto const& i : cpp2_range ) { do {
200200
std::vector vj {0, 1, 2};
201-
for ( auto&& cpp2_range = std::as_const(vj); auto const& j : cpp2_range ) {{
201+
for ( auto const& cpp2_range = vj; auto const& j : cpp2_range ) {{
202202
std::cout << i << j << " ";
203203
if (j == 1) {
204204
goto CONTINUE_166_9;
@@ -214,9 +214,9 @@ auto for_continue_outer() -> void
214214
{
215215
std::vector vi {0, 1, 2};
216216
auto counter {0};
217-
for ( auto&& cpp2_range = std::as_const(vi); auto const& i : cpp2_range ) { { do {
217+
for ( auto const& cpp2_range = vi; auto const& i : cpp2_range ) { { do {
218218
std::vector vj {0, 1, 2};
219-
for ( auto&& cpp2_range = std::as_const(vj); auto const& j : cpp2_range ) {
219+
for ( auto const& cpp2_range = vj; auto const& j : cpp2_range ) {
220220
std::cout << i << j << " ";
221221
if (j == 1) {
222222
goto CONTINUE_182_5;
@@ -232,9 +232,9 @@ auto for_break_inner() -> void
232232
{
233233
std::vector vi {0, 1, 2};
234234
auto counter {0};
235-
for ( auto&& cpp2_range = std::as_const(vi); auto const& i : cpp2_range ) { do {
235+
for ( auto const& cpp2_range = vi; auto const& i : cpp2_range ) { do {
236236
std::vector vj {0, 1, 2};
237-
for ( auto&& cpp2_range = std::as_const(vj); auto const& j : cpp2_range ) {{
237+
for ( auto const& cpp2_range = vj; auto const& j : cpp2_range ) {{
238238
std::cout << i << j << " ";
239239
if (j == 1) {
240240
goto BREAK_202_9;
@@ -250,9 +250,9 @@ auto for_break_outer() -> void
250250
{
251251
std::vector vi {0, 1, 2};
252252
auto counter {0};
253-
for ( auto&& cpp2_range = std::as_const(vi); auto const& i : cpp2_range ) { { do {
253+
for ( auto const& cpp2_range = vi; auto const& i : cpp2_range ) { { do {
254254
std::vector vj {0, 1, 2};
255-
for ( auto&& cpp2_range = std::as_const(vj); auto const& j : cpp2_range ) {
255+
for ( auto const& cpp2_range = vj; auto const& j : cpp2_range ) {
256256
std::cout << i << j << " ";
257257
if (j == 1) {
258258
goto BREAK_218_5;

source/cppfront.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -1544,14 +1544,13 @@ class cppfront
15441544
else if (*n.identifier == "for") {
15451545
assert(!n.condition && !n.statement && n.range && n.body);
15461546

1547-
printer.print_cpp2("for ( auto&& cpp2_range = ", n.position());
15481547
if (n.for_with_in) {
1549-
printer.print_cpp2("std::as_const(", n.position());
1548+
printer.print_cpp2("for ( auto const& cpp2_range = ", n.position());
15501549
}
1551-
emit(*n.range);
1552-
if (n.for_with_in) {
1553-
printer.print_cpp2(")", n.position());
1550+
else {
1551+
printer.print_cpp2("for ( auto&& cpp2_range = ", n.position());
15541552
}
1553+
emit(*n.range);
15551554
printer.print_cpp2("; ", n.position());
15561555
emit(*n.get_for_parameter());
15571556
printer.print_cpp2(" : cpp2_range ) ", n.position());

0 commit comments

Comments
 (0)