How can I feed tuples?

I am trying to create a variation container template that stores a set of element vectors. The point of this container is the elements of all vectors, all related, and I want to maintain this correlation later, but this is not necessary for calculation. Imagine, if you want, vector_3 and ref_id of some kind.

The container will only mutate the vectors evenly together. Therefore, the parts that I understand look like this:

template<typename ...Elems>
class container
{
    std::tuple<std::vector<Elems>...> data_;

public:
    template<typename I>
    const typename std::tuple_element<I, data_type>::type &nth_index() const
    { return std::get<I>(data_); }
};

I am struggling with the insertion method. I thought something like:

void push_back(std::tuple<Elems...> &values)
{
    std::tuple<std::back_insert_iterator<std::vector<Elems>>...> inserters;
}

But I do not know how to initialize this tuple of "inserters". I looked at various examples of recursive patterns here in stackoverflow, and I can't keep it all in my head long enough to figure it out.

I assumed that if I had such a tuple, I could use a simple assignment:

inserters = values;

, :

std::tuple<Elems &...> operator[](const size_t index)
{
     ...
}

, , .

, - , , . 0x. . MSVC 2012.

+4
2

++ 11 SFINAE :

template<typename ...Elems>
class container {
  std::tuple<std::vector<Elems>...> data_;

  template<std::size_t N>
  typename std::enable_if<(N <std::tuple_size<decltype(data_)>::value), int>::type
  push_back_impl(std::tuple<Elems...> const &values) {
    std::get<N>(data_).push_back(std::get<N>(values));
    return push_back_impl<N + 1>(values);
  }

  template<std::size_t N>
  typename std::enable_if<(N == std::tuple_size<decltype(data_)>::value), int>::type
  push_back_impl(std::tuple<Elems...> const &values) {
    return 0;
  }

public:

  void push_back(std::tuple<Elems...> const &values) {
     push_back_impl<0>(values);
  }

};

Live Demo

, , SO answer:

template <size_t ...I>
struct index_sequence {};

template <size_t N, size_t ...I>
struct make_index_sequence : public make_index_sequence<N - 1, N - 1, I...> {};

template <size_t ...I>
struct make_index_sequence<0, I...> : public index_sequence<I...> {};

template<typename ...Elems>
class container {
  std::tuple<std::vector<Elems>...> data_;

  template<size_t ...I>
  std::tuple<Elems&...> access_impl(std::size_t const idx, index_sequence<I...>) {
    return std::tie(std::get<I>(data_)[idx]...);
  }

public:

  std::tuple<Elems&...> operator[](std::size_t const idx) {
    return access_impl(idx, make_index_sequence<sizeof...(Elems)>());
  }
};

Live Demo

+1
#include <vector>
#include <tuple>
#include <cstddef>
#include <utility>

template <typename... Elems>
class container
{
    using data_type = std::tuple<std::vector<Elems>...>;

    data_type data_;

public:    
    template <std::size_t I>
    const typename std::tuple_element<I, data_type>::type& nth_index() const
    { return std::get<I>(data_); }

    void push_back(const std::tuple<Elems...>& values)
    {
        return push_back(std::make_index_sequence<sizeof...(Elems)>{}, values);
    }    

    std::tuple<Elems&...> operator[](std::size_t index)
    {
        return get_elems(std::make_index_sequence<sizeof...(Elems)>{}, index);
    }

private:
    template <std::size_t... Is>
    void push_back(std::index_sequence<Is...>, const std::tuple<Elems...>& values)
    {
        using expand = int[];
        static_cast<void>(expand{ 0, (std::get<Is>(data_).push_back(std::get<Is>(values)), 0)... });
    }

    template <std::size_t... Is>
    std::tuple<Elems&...> get_elems(std::index_sequence<Is...>, std::size_t index)
    {
        return std::forward_as_tuple(std::get<Is>(data_)[index]...);
    }
};

DEMO

+3

Source: https://habr.com/ru/post/1629058/


All Articles