3 ms·
Is it, though? x.a and x.b, and a and b, name different things. Why should they be correlated? Apart from ML, I which pattern matching works differently, I don
by andrepd 8y ago
Is it, though? x.a and x.b, and a and b, name different things. Why should they be correlated? Apart from ML, I which pattern matching works differently, I don't know of any language where destructuring tuples tries to match names rather than position (even in ML, if you pattern match tuples rather than records).
- cannam 8y agoBecause 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 ...