vendredi 2 octobre 2015

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
};

V< int, double, bool > v{1, 0.0, false};
static_assert(std::is_same< decltype(v[0]), int >{});
static_assert(std::is_same< decltype(v[1]), double >{});
static_assert(std::is_same< decltype(v[2]), bool >{});

template< typename ...types >
V< types... > ctor(types &&... args)
{ return {std::forward< types >(args)...}; }

auto f = 0.0;
auto const b = false;
auto w = ctor(1, f, b, std::move(b));
static_assert(std::is_same< decltype(w[0]), int >{});
static_assert(std::is_same< decltype(w[1]), double & >{});
static_assert(std::is_same< decltype(w[2]), bool const & >{});
static_assert(std::is_same< decltype(w[3]), bool const >{});

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

Aucun commentaire:

Enregistrer un commentaire