Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variadic aggregates as core language feature

std::tuple is highly template-loaded beast. To access to n-th member compiler must perform a plenty of template instantiations, although its simple nature: access to n-th data member of corresponding imaginary struct. It seems that std::tuple should be a core language feature, something like this (pseudocode):

template< typename ...types >
struct/* or class, or even union */ V
{
    types... V; // defines implicitly `operator [/*constant expression*/]` to access by index
    // if more than one variadic parameter pack provided
    // (during expanding of parameter pack of template 
    // parameters in specializations) then instead of
    // `V` there can be specific data-member name (say, `x`),
    // but still with `x.operator []` implicitly defined

    // member functions and data members allowed
};

template< typename ...types >
V< std::decay_t< types >... > make_tuple(types &&... args)
{ return {std::forward< types >(args)...}; }
template< typename ...types >
V< types &&... > forward_as_tuple(types &&... args)
{ return {std::forward< types >(args)...}; }
template< typename ...types >
V< types &... > tie(types &... args)
{ return {args...}; }

Is there any proposal of something like language supported variadic data-members definition syntax for classes?

like image 831
Tomilov Anatoliy Avatar asked Oct 02 '15 11:10

Tomilov Anatoliy


1 Answers

See N4235 Selecting from Parameter Packs for a related idea.

That might be useful for std::tuple, but I'm more interested in a feature to simplify its construction, not selection of members (which is comparatively simple).

The definition of std::tuple is insanely complicated, in order to make all the constructors correctly model the properties of all the members (see N4064 for the most recent changes in that area).

When you use aggregate-initialization the compiler automatically checks whether each member of the aggregate can be constructed from the corresponding initializers. It checks for suitable constructors, whether they are explicit, accept the relevant lvalue/rvalue category etc.

When you define a constructor for std::tuple is has to be very complicated to ensure that only valid conversions happen, that explicit constructors are not used when they shouldn't be etc.

I'd like a feature that made it much easier to automatically generate suitable constructors to model the same semantics as you get for free from aggregate initialization.

like image 56
Jonathan Wakely Avatar answered Nov 06 '22 05:11

Jonathan Wakely