https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96170
Bug ID: 96170
Summary: Enhancement: allow requires-clause checking of
statement expression substitution success or failure
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: wjwray at gmail dot com
Target Milestone: ---
Some sort of sfinae on statement expressions containing structured bindings
can provide a convenient way to detect the number of bindings in a type.
Checking in requires clauses seems like the most direct approach:
https://godbolt.org/z/Eoq9rr
template <typename T> constexpr int arity()
{
if constexpr (requires (T& a) { ({auto [x] = a;}); }) return 1;
else if constexpr (requires (T& a) { ({auto [x,y] = a;}); }) return 2;
// ... etc ... 3,4,5 ... N
else
return 0;
};
G++ accepts the template and,
for a type T that decomposes into 1 element, arity<T>() == 1 succeeds.
However, it fails eagerly on the 1st condition for any other type,
e.g. arity<int>(), arity<int[2]>(), &etc...
c.f. bug 92402 seeking to match Clang's ability to sfinae in other contexts
(Clang accepts the template above but returns 0 for all T.)