如何将同构 std::tuple 转换为 std::array?

问题描述 投票:0回答:7

如果我有

std::tuple<double, double, double, ...>
(其中类型是同质的),是否有一个股票函数或构造函数可以转换为
std::array<double, N>

我能够使用递归模板代码(我的草稿答案发布在下面)来让它工作,但我更喜欢其他人的,希望有更好的解决方案。

c++ arrays c++11 tuples stdarray
7个回答
27
投票

在不使用递归的情况下将元组转换为数组,包括使用完美转发(对于仅移动类型有用):

#include <iostream>
#include <tuple>
#include <array>

template<int... Indices>
struct indices {
    using next = indices<Indices..., sizeof...(Indices)>;
};

template<int Size>
struct build_indices {
    using type = typename build_indices<Size - 1>::type::next;
};

template<>
struct build_indices<0> {
    using type = indices<>;
};

template<typename T>
using Bare = typename std::remove_cv<typename std::remove_reference<T>::type>::type;

template<typename Tuple>
constexpr
typename build_indices<std::tuple_size<Bare<Tuple>>::value>::type
make_indices()
{ return {}; }

template<typename Tuple, int... Indices>
std::array<
  typename std::tuple_element<0, Bare<Tuple>>::type,
    std::tuple_size<Bare<Tuple>>::value
>
to_array(Tuple&& tuple, indices<Indices...>)
{
    using std::get;
    return {{ get<Indices>(std::forward<Tuple>(tuple))... }};
}

template<typename Tuple>
auto to_array(Tuple&& tuple)
-> decltype( to_array(std::declval<Tuple>(), make_indices<Tuple>()) )
{
    return to_array(std::forward<Tuple>(tuple), make_indices<Tuple>());
}

int main() {
  std::tuple<double, double, double> tup(1.5, 2.5, 4.5);
  auto arr = to_array(tup);
  for (double x : arr)
    std::cout << x << " ";
  std::cout << std::endl;
  return 0;
}

15
投票

C++17 解决方案很简短:

template<typename tuple_t>
constexpr auto get_array_from_tuple(tuple_t&& tuple)
{
    constexpr auto get_array = [](auto&& ... x){ return std::array{std::forward<decltype(x)>(x) ... }; };
    return std::apply(get_array, std::forward<tuple_t>(tuple));
}

将其用作

auto tup = std::make_tuple(1.0,2.0,3.0);
auto arr = get_array_from_tuple(tup);

编辑:忘记在任何地方撒

constexpr
:-)


9
投票

您可以非递归地进行:

#include <array>
#include <tuple>
#include <redi/index_tuple.h>  // see below

template<typename T, typename... U>
  using Array = std::array<T, 1+sizeof...(U)>;

template<typename T, typename... U, unsigned... I>
  inline Array<T, U...>
  tuple_to_array2(const std::tuple<T, U...>& t, redi::index_tuple<I...>)
  {
    return Array<T, U...>{ std::get<I>(t)... };
  }

template<typename T, typename... U>
  inline Array<T, U...>
  tuple_to_array(const std::tuple<T, U...>& t)
  {
    using IndexTuple = typename redi::make_index_tuple<1+sizeof...(U)>::type;
    return tuple_to_array2(t, IndexTuple());
  }

请参阅https://gitlab.com/redistd/redistd/blob/master/include/redi/index_tuple.h了解我对index_tuple的实现,类似的东西对于使用元组和类似的可变参数模板非常有用。类似的实用程序在 C++14 中被标准化为

std::index_sequence
(有关独立的 C++11 实现,请参阅 index_seq.h)。


7
投票

我会返回数组,而不是通过引用填充它,这样

auto
就可以用来使调用站点更干净:

template<typename First, typename... Rem>
std::array<First, 1+sizeof...(Rem)>
fill_array_from_tuple(const std::tuple<First, Rem...>& t) {
  std::array<First, 1+sizeof...(Rem)> arr;
  ArrayFiller<First, decltype(t), 1+sizeof...(Rem)>::fill_array_from_tuple(t, arr);
  return arr;
}

// ...

std::tuple<double, double, double> tup(0.1, 0.2, 0.3);
auto arr = fill_array_from_tuple(tup);

实际上,NRVO 将消除大多数性能问题。


4
投票
#include <iostream>
#include <tuple>
#include <array>

template<class First, class Tuple, std::size_t N, std::size_t K = N>
struct ArrayFiller {
  static void fill_array_from_tuple(const Tuple& t, std::array<First, N> & arr) {
    ArrayFiller<First, Tuple, N, K-1>::fill_array_from_tuple(t, arr);
    arr[K-1] = std::get<K-1>(t);
  }
};

template<class First, class Tuple, std::size_t N>
struct ArrayFiller<First, Tuple, N, 1> {
  static void fill_array_from_tuple( const Tuple& t, std::array<First, N> & arr) {
    arr[0] = std::get<0>(t);
  }
};

template<typename First, typename... Rem>
void fill_array_from_tuple(const std::tuple<First, Rem...>& t, std::array<First, 1+sizeof...(Rem)> & arr) {
  ArrayFiller<First, decltype(t), 1+sizeof...(Rem)>::fill_array_from_tuple(t, arr);
}

int main() {
  std::tuple<double, double, double> tup(0.1, 0.2, 0.3);
  std::array<double, 3> arr;
  fill_array_from_tuple(tup, arr);

  for (double x : arr)
    std::cout << x << " ";
  return 0;
}

4
投票

即使标题说 C++11 我认为 C++14 解决方案值得分享(因为每个寻找问题的人都会在这里出现)。这个可以在编译时使用(

constexpr
正确)并且比其他解决方案短得多。

#include <array>
#include <tuple>
#include <utility>
#include <iostream>

// Convert tuple into a array implementation
template<typename T, std::size_t N, typename Tuple,  std::size_t... I>
constexpr decltype(auto) t2a_impl(const Tuple& a, std::index_sequence<I...>)
{
        return std::array<T,N>{std::get<I>(a)...};
}

// Convert tuple into a array
template<typename Head, typename... T>
constexpr decltype(auto) t2a(const std::tuple<Head, T...>& a)
{
        using Tuple = std::tuple<Head, T...>;
        constexpr auto N = sizeof...(T) + 1;
        return t2a_impl<Head, N, Tuple>(a, std::make_index_sequence<N>());
}

int main()
{
    constexpr auto tuple = std::make_tuple(-1.3,2.1,3.5);
    constexpr auto array = t2a(tuple);

    static_assert(array.size() == 3, "err");

    for(auto k : array)
        std::cout << k << ' ';
    return 0;
}

示例


1
投票

在 C++14 中,你可以这样做来生成数组:

auto arr = apply([](auto... n){return std::array<double, sizeof...(n)>{n...};}, tup);

完整代码:

#include<experimental/tuple> // apply
#include<cassert>

//using std::experimental::apply; c++14 + experimental
using std::apply; // c++17

template<class T, class Tuple>
auto to_array(Tuple&& t){
    return apply([](auto... n){return std::array<T, sizeof...(n)>{n...};}, t); // c++14 + exp
}

int main(){
    std::tuple<int, int, int> t{2, 3, 4};

    auto a = apply([](auto... n){return std::array<int, 3>{n...};}, t); // c++14 + exp
    assert( a[1] == 3 );

    auto a2 = apply([](auto... n){return std::array<int, sizeof...(n)>{n...};}, t); // c++14 + exp
    assert( a2[1] == 3 );

    auto a3 = apply([](auto... n){return std::array{n...};}, t); // c++17
    assert( a3[1] == 3 );

    auto a4 = to_array<int>(t);
    assert( a4[1] == 3 );

}

注意,微妙的问题是,当源元组中的所有类型都不相同时该怎么办,是否会编译失败?使用隐式转换规则?使用显式转换规则?

© www.soinside.com 2019 - 2024. All rights reserved.