jamesjuett / lobster

Interactive Program Visualization Tools
8 stars 3 forks source link

C++ Support - Range-based for loops #318

Open jamesjuett opened 2 years ago

jamesjuett commented 2 years ago

Add support for C++ range-based for loops, e.g.

High level tasks:

Range-based for loops work by attempting behind-the-scenes calls to std::begin() and std::end(). These can't be literally reconstructed in C++ since they depend on templates and template specialization, which is not supported. However, separate overloads of these functions can be defined for arrays and vectors, and the range-based for loop construct can compile without necessarily needing to go through these functions at all (can just check whether it's an array, vector, or class and then proceed accordingly).

int arr[5] = {1, 2, 3, 4, 5};
for(int i : arr) {
  cout << i << endl;
}

vector<int> vec = {1, 2, 3, 4, 5};
for(int &i : vec) {
  i += 1;
}