10 ms·
Because this is a struct, not a tuple. Historically at least, I expect C/C++ structure elements to be identified by name rather than by declaration order. Chang
by cannam 8y ago
Because this is a struct, not a tuple. Historically at least, I expect C/C++ structure elements to be identified by name rather than by declaration order. Changing the declaration order ideally would not silently change the meaning of code that uses the struct.
I realise that C++11 structure initialisation syntax already blurs this distinction. That's already a bit dangerous in my view, although in both cases I can understand why they chose to do it that way.
Are there other languages in which struct (not tuple) bindings happen by declaration order?
- jcelerier 8y ago> Historically at least, I expect structure elements to be identified by name rather than by declaration order. then... just use the struct directly if that's what you want ? But a single binding can map to multiple structs, especially in generic code : template<typename T> double dist(T point) { auto& [x, y] = point; return std::sqrt(x * x + y * y); } should work with T's that look like struct Point { float f[2]; } or struct Point { float x, y; } or struct Point { float p1, p2; } or std::tuple<float, float> or ...