I want to define a concept that can detect whether or not the type T
can be assigned to a structured binding:
template <typename T>
concept two_elements_structured_bindable = requires (T t) {
auto [x, y] = t;
};
but this cannot be compiled. Is there a proper way to define a concept like that?
No.
There are three cases in structured bindings:
Arrays. That's easy enough to detect.
Tuple-like. You can easily check if std::tuple_size<E>::value
is valid, and then check if std::tuple_element<I, E>::type
is valid as a type for all the right types. But the get
case is harder since you have to deal with member vs non-member... but otherwise I think doable.
Types which have all public (yeah yeah, technically accessible) members as direct members of the same class. This is impossible to detect with current technology. magic_get
can, I think, handle struct X { int a, b; };
but neither struct Y : X { };
nor struct Z { X& x; };
You would need to have proper reflection to check this case.
As of C++20, you would need some kind of compiler intrinsic to do this.