struct B {
int w;
int z;
};
namespace std {
template<typename T> struct tuple_size {
};
template<>
struct tuple_size<B> {
static constexpr int value = 2;
};
}
int main() {
auto [x, y] = B{1, 2};
return 0;
}
We get:
decomp.cpp:16:11: error: use of undeclared identifier 'get'
16 | auto [x, y] = B{1, 2};
| ^
decomp.cpp:16:11: note: in implicit initialization of binding declaration 'x'
1 error generated.
Compare this to the more helpful error message we get when we do actually implement the get operation:
struct B {
int w;
int z;
template<int> int get();
template<> int get<0>() { return w; }
template<> int get<1>() { return z; }
};
$ g++ decomp.cpp -std=c++2a
decomp.cpp:19:11: error: cannot decompose this type; 'std::tuple_element<0UL, B>::type' does not name a type
19 | auto [x, y] = B{1, 2};
| ^
decomp.cpp:19:11: note: in implicit initialization of binding declaration 'x'
1 error generated.
I found the second diagnostic to be better at nudging me in the write direction as to what's missing.
Not sure how actionable this is. I just ran into this when trying to write a test for a tuple-like decomposition of a custom type without including the tuple header.
Take this contrived example:
```
struct B {
int w;
int z;
};
namespace std {
template<typename T> struct tuple_size {
};
template<>
struct tuple_size<B> {
static constexpr int value = 2;
};
}
int main() {
auto [x, y] = B{1, 2};
return 0;
}
```
We get:
```
decomp.cpp:16:11: error: use of undeclared identifier 'get'
16 | auto [x, y] = B{1, 2};
| ^
decomp.cpp:16:11: note: in implicit initialization of binding declaration 'x'
1 error generated.
```
Compare this to the more helpful error message we get when we do actually implement the `get` operation:
```
struct B {
int w;
int z;
template<int> int get();
template<> int get<0>() { return w; }
template<> int get<1>() { return z; }
};
$ g++ decomp.cpp -std=c++2a
decomp.cpp:19:11: error: cannot decompose this type; 'std::tuple_element<0UL, B>::type' does not name a type
19 | auto [x, y] = B{1, 2};
| ^
decomp.cpp:19:11: note: in implicit initialization of binding declaration 'x'
1 error generated.
```
I found the second diagnostic to be better at nudging me in the write direction as to what's missing.
Not sure how actionable this is. I just ran into this when trying to write a test for a tuple-like decomposition of a custom type without including the `tuple` header.
Take this contrived example:
We get:
Compare this to the more helpful error message we get when we do actually implement the
get
operation:I found the second diagnostic to be better at nudging me in the write direction as to what's missing.
Not sure how actionable this is. I just ran into this when trying to write a test for a tuple-like decomposition of a custom type without including the
tuple
header.