I have something like:
Array definition:
array<array<Value, N_FOO_PER_BAR>, N_BAR> arr;
Access function:
Value getFoo(int barIdx, int fooIdx){
return arr[barIdx][fooIdx];
}
For-loop:
for(int i = 0; i < N_BAR; i ++){
for(int j = 0; j < N_FOO_PER_BAR; j ++){
arr[i][j].doSomething();
}
}
Problem: Indices for foo and bar can easily get mixed up when using getFoo(...)
.
I would like to define a type BarIdx
and FooIdx
and then the compiler should complain when I mix them up.
The access function would then look like:
function getFoo(BadIdx barIdx, FooIdx fooIdx){
return arr[barIdx][fooIdx];
}
The for-loop could look like:
for(BarIdx i = 0; i < N_BAR; i ++){
for(FooIdx j = 0; j < N_FOO_PER_BAR; j ++){
arr[i][j].doSomething();
}
}
So I'm looking how to define a type which (a) will issue a warning/error when used at the wrong place but (b) still behaves as much as an integer as possible, for for loop and array access with []. This should also work on CUDA, so I would prefer to use simple language constructs rather than a perfect solution.