fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<int N>
  5. void increment(int (&v)[N])
  6. {
  7. // No problem
  8. int w[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 9 };
  9. for (int& x : w){
  10. std::cout << "range-for-statement: " << ++x << "\n";
  11. }
  12.  
  13. // error: cannot build range expression with array function
  14. // parameter 'v' since parameter with array type 'int []' is
  15. // treated as pointer type 'int *'
  16. for (int x : v){
  17. std::cout << "printing " << x << "\n";
  18. }
  19.  
  20. // No problem
  21. for (int i = 0; i < 10; i++){
  22. int* p = &v[i];
  23. }
  24. }
  25.  
  26. int main()
  27. {
  28. int v[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 9 };
  29. increment(v);
  30. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
range-for-statement: 10
range-for-statement: 9
range-for-statement: 8
range-for-statement: 7
range-for-statement: 6
range-for-statement: 5
range-for-statement: 4
range-for-statement: 3
range-for-statement: 2
range-for-statement: 10
printing 9
printing 8
printing 7
printing 6
printing 5
printing 4
printing 3
printing 2
printing 1
printing 9