I am attempting at access a class that is like a multidimensional array through the operator[].. only to discover that a comma separated set of parameters is reduced to the first parameter only.. This was a quite a surprise to me..
Does anyone know if this an actual bug or some kind of legalize gotcha in the c++ standard or compiler implementations?..
Its rather annoying as the compiler tends to reduce it without error or warning and bugs can slide by if the coder makes a mistake.
#include <memory>
#include <vector>
#include <stdexcept>
#include <iostream>
template <size_t D, size_t N>
struct A {
//size_t operator[](int idx) {
// throw std::runtime_error("wtf!");
//}
size_t operator[](std::initializer_list<int> idx) {
if (idx.size() != D) {
throw std::runtime_error("lacking entries");
}
size_t offset = 0;
for (size_t v : idx) {
offset = offset*N + v;
}
return offset;
}
};
int main() {
A<3,10> a;
std::cout << a[1,2,3] << "n"; // BUG??
std::cout << a[{1,2,3}] << "n";
}
The godbolt is here: https://godbolt.org/z/WYeGn7Y4n
I expect a[1,2,3]
to access the initializer list call and pass all parameters over however it accesses the single parameter one(or tries to)
1