About std :: tuple, and how to use it as a member of a class

1

I have an implementation that works for me in the following way:

// clase de enteros modulo el parametro N
template<unsigned N> int_mod_N {
    unsigned m_i; // unico datro miembro
    /* ... implementacion ...*/
};

// clase que es una "tupla" de los anteriores enteros modulo N
// pero cada posicion de la tupla puede tener un modulo diferente
template<unsigned N_n,unsigned ... N_nm1>
struct producto_cartesiano_enteros_modulos_Ns {
    int_mod_N<N_n> m_msd;
    producto_cartesiano_enteros_modulos_Ns<N_nm1...> m_resto;
    // no hay mas datos miembros
    // implementacion utilizando recursion
};

The problem is that everything I do is already done in < tuple & gt ;. However I do not know how to implement the constructors, etc. I also hoped that I could use the entire tuple at the same time, without having to delegate recursion. But there is no tutorial on youtube or writing that I can understand: I do not understand the use of std :: integer_sequence , nor that of < em> std :: index_sequence , which should be helpful. What I'm trying is a template of this type:

// clase que es una "tupla" de los enteros modulo N
// cada posicion de la tupla puede tener un modulo diferente
template<unsigned N_n,unsigned ... N_nm1>
struct producto_cartesiano_enteros_modulos_Ns {
    std::tuple<int_mod_N<N_n>,int_mod_N<N_nm1>...> m_d;
    // no hay mas datos miembros
    // implementacion ????
};

I appreciate any idea, or tell me somewhere where to see something that will help me. Thanks.

    
asked by Eärendil Elrond Arwen 31.07.2018 в 21:02
source

1 answer

0

I do not fully understand the question, it would be good to have a practical case of use. With what I understood, I think you're looking for this:

template<unsigned ... N>
struct producto_cartesiano_enteros_modulos_Ns {
    std::tuple<int_mod_N<N>...> m_tupla;

    template<typename ... V>
    producto_cartesiano_enteros_modulos_Ns(V ... v) :
        m_tupla{std::tuple<int_mod_N<N>...>{int_mod_N<N>{static_cast<unsigned>(v)}...}} {}
};

Your Cartesian product has as a member a tuple of integers module, the constructor receives an undetermined number of parameters that will be converted to unsigned to build the internal tuple. It can be used like this:

template <unsigned U>
std::ostream &operator <<(std::ostream &o, const int_mod_N<U> &imN)
{
    return o << "MOD<" << U << ">(" << imN.m_i << ')';
}

template<unsigned ... N>
constexpr std::ostream &operator <<(std::ostream &o, const producto_cartesiano_enteros_modulos_Ns<N...> &pcem)
{
    auto print = [&](auto &t){ o << t << ' '; };
    (print(std::get<int_mod_N<N>>(pcem.m_tupla)), ...);
    return o;
}

int main()
{
    producto_cartesiano_enteros_modulos_Ns<1,2,3,4,5> pcem(5,4,3,2,1);
    std::cout << pcem;
    return 0;
}

The previous code shows:

MOD<1>(5) MOD<2>(4) MOD<3>(3) MOD<4>(2) MOD<5>(1)

You can see the code working in Wandbox , but keep in mind that:

  • The number of template arguments in producto_cartesiano_enteros_modulos_Ns must match the number of arguments in its constructor.
  • The parameters passed to the constructor of producto_cartesiano_enteros_modulos_Ns must be convertible to unsigned .
  • Operator overload << for producto_cartesiano_enteros_modulos_Ns only works if the template arguments are all different, otherwise there is an ambiguity in std::get .
answered by 01.08.2018 / 09:33
source