Is it possible to determine how many variable names should I to specify in square brackets using structured bindings syntax to match the number of data members of a plain right hand side struct
?
I want to make a part of generic library, which uses structured bindings to decompose arbitrary classes into its constituents. At the moment there is no variadic version of structured bindings (and, I think, cannot be for current syntax proposed), but my first thought is to make a set of overloadings of some function decompose()
, which performs decomposition of struct
parameter into a set of its constituents. decompose()
should be overloaded by number of parameter's (which is struct
) data members. Currently constexpr if
syntax also can be used to dispatch this. But how can I emulate something similar to sizeof...
operator for above purposes? I can't use auto [a, b, c]
syntax somewhere in SFINAE constructions, because it is a decomposition declaration and AFAIK any declaration cannot be used inside decltype
, also I cannot use it for my purposes in the body of lambda functions because lambda functions cannot be used inside template arguments too.
Surely I want to have builtin operator (with syntax like sizeof[] S
/sizeof[](S)
for class S
), but something like the following is also would be acceptable:
template< typename type, typename = void >
struct sizeof_struct
{
};
template< typename type >
struct sizeof_struct< type, std::void_t< decltype([] { auto && [p1] = std::declval< type >(); void(p1); }) > >
: std::integral_constant< std::size_t, 1 >
{
};
template< typename type >
struct sizeof_struct< type, std::void_t< decltype([] { auto && [p1, p2] = std::declval< type >(); void(p1); void(p2); }) > >
: std::integral_constant< std::size_t, 2 >
{
};
... etc up to some reasonable arity
Maybe constexpr
lambda will allow us to use them into template's arguments. What do you think?
Will it be possible with coming Concepts?
See Question&Answers more detail:os