结构到std::元组的转换

struct to/from std::tuple conversion

本文关键字:转换 元组 结构 std      更新时间:2023-10-16

假设我有相同类型布局的structstd::tuple

struct MyStruct { int i; bool b; double d; }
using MyTuple = std::tuple<int,bool,double>;

有什么标准化的方法可以把一个投给另一个吗?

附言:我知道琐碎的内存复制可以做到这一点,但它是与对齐和实现相关的

我们可以使用结构化绑定将结构转换为元组,只需一些工作。

结构到元组非常尴尬。

template<std::size_t N>
struct to_tuple_t;
template<>
struct to_tuple_t<3> {
  template<class S>
  auto operator()(S&& s)const {
    auto[e0,e1,e2]=std::forward<S>(s);
    return std::make_tuple(e0, e1, e2);
  }
};

现在,为您想要支持的每个大小写一个to_tuple_t。这会变得乏味。遗憾的是,我不知道如何在那里引入参数包。

template<std::size_t N, class S>
auto to_tuple(S&& s) {
  return to_tuple_t<N>{}(std::forward<S>(s));
}

我也不知道如何计算所需的N的值。所以当你调用它时,你必须在auto t = to_tuple<3>(my_struct);中键入3

我不是结构化绑定的大师。可能有一个&&&或decltype可以在以下线路上进行完美转发:

    auto[e0,e1,e2]=std::forward<S>(s);
    return std::make_tuple(e0, e1, e2);

但如果没有编译器,我会保守一些,做多余的副本。


将元组转换为结构很容易:

template<class S, std::size_t...Is, class Tup>
S to_struct( std::index_sequence<Is...>, Tup&& tup ) {
  using std::get;
  return {get<Is>(std::forward<Tup>(tup))...};
}
template<class S, class Tup>
S to_struct( Tup&&tup ) {
  using T=std::remove_reference_t<Tup>;
  return to_struct(
    std::make_index_sequence<std::tuple_size<T>{}>{},
    std::forward<Tup>(tup)
  );
}

基于tuple_size的SFINAE支持可能对to_struct有利。

上面的代码适用于所有类似元组的代码,如std::pairstd::array,以及任何您自定义的支持结构化绑定的代码(tuple_sizeget<I>)。


有趣的是,

std::array<int, 3> arr{1,2,3};
auto t = to_tuple<3>(arr);

工作并返回一个包含3个元素的元组,因为to_tuple是基于结构化绑定的,这些绑定使用tuple-like作为输入。

CCD_ 16是该家族中的另一种可能性。

不幸的是,没有自动的方法可以做到这一点,但另一种选择是将结构调整为Boost.Fusion序列。对于每一个新班级,你都要一劳永逸地做到这一点。

#include <boost/fusion/adapted/struct/adapt_struct.hpp>
...
struct MyStruct { int i; bool b; double d; }
BOOST_FUSION_ADAPT_STRUCT(
    MyStruct,
    (int, i)
    (bool, b)
    (double, d)
)

使用MyStruct就像它在Fusion.Sequence中一样(如果你使这些函数通用,它几乎适用于你已经使用std::tuple<...>的所有地方。)作为奖励,你根本不需要复制你的数据成员。

如果真的需要转换为std::tuple,在"融合适应"之后,你可以这样做:

#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/algorithm/transformation/zip.hpp>
...
auto to_tuple(MyStruct const& ms){
   std::tuple<int, bool, double> ret;
   auto z = zip(ret, ms);
   boost::fusion::for_each(z, [](auto& ze){get<0>(ze) = get<1>(ze);});
   // or use boost::fusion::copy
   return ret;
}

事实上,std::tuple是一个半支持的特性。这就像有STD容器而没有算法。幸运的是,我们有#include <boost/fusion/adapted/std_tuple.hpp>,它可以让我们做一些了不起的事情。

完整代码:

通过包含来自Boost.Fusion的std_tuple.hpp标头,std::tuple会自动适应Boost.FFusion序列,因此使用Boost.Fersion作为结构和std::tuple之间的桥梁可以实现以下操作:

#include <iostream>
#include <string>
#include <tuple>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/algorithm/auxiliary/copy.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
struct foo
{
  std::string a, b, c;
  int d, e, f;
};
BOOST_FUSION_ADAPT_STRUCT(
    foo,
    (std::string, a)
    (std::string, b)
    (std::string, c)
    (int, d)
    (int, e)
    (int, f)
)
template<std::size_t...Is, class Tup>
foo to_foo_aux(std::index_sequence<Is...>, Tup&& tup) {
  using std::get;
  return {get<Is>(std::forward<Tup>(tup))...};
}
template<class Tup>
foo to_foo(Tup&& tup) {
  using T=std::remove_reference_t<Tup>;
  return to_foo_aux(
    std::make_index_sequence<std::tuple_size<T>{}>{},
    std::forward<Tup>(tup)
  );
}
template<std::size_t...Is>
auto to_tuple_aux( std::index_sequence<Is...>, foo const& f ) {
  using boost::fusion::at_c;
  return std::make_tuple(at_c<Is>(f)...);
}
auto to_tuple(foo const& f){
  using T=std::remove_reference_t<foo>;
  return to_tuple_aux(
    std::make_index_sequence<boost::fusion::result_of::size<foo>::type::value>{},
    f
  );    
}
int main(){

    foo f{ "Hello", "World", "!", 1, 2, 3 };
    std::tuple<std::string, std::string, std::string, int, int, int> dest = to_tuple(f);
    // boost::fusion::copy(f, dest); // also valid  but less general than constructor
    std::cout << std::get<0>(dest) << ' ' << std::get<1>(dest) << std::get<2>(dest) << std::endl;
    std::cout << at_c<0>(dest) << ' ' << at_c<1>(dest) << at_c<2>(dest) << std::endl; // same as above
    foo f2 = to_foo(dest);
    std::cout << at_c<0>(f2) << ' ' << at_c<1>(f2) << at_c<2>(f2) << std::endl;
}

我不会推荐reinterpret_cast<std::tuple<...>&>(mystructinstance.i),因为这会导致负面投票,而且它不可移植。

有什么标准化的方法可以把一个投给另一个吗?

没有办法将一个"铸造"到另一个。

最简单的可能是使用CCD_ 26将元组打包到CCD_;

struct MyStruct { int i; bool b; double d; };
using MyTuple = std::tuple<int,bool,double>;
auto t = std::make_tuple(42, true, 5.1);
MyStruct s;
std::tie(s.i, s.b, s.d) = t;

演示。

您可以进一步将其封装在更高级别的宏或"生成器"(make样式)函数中,例如;

std::tuple<int, bool, double> from_struct(MyStruct const& src)
{
  return std::make_tuple(src.i, src.b, src.d);
}
MyStruct to_struct(std::tuple<int, bool, double> const& src)
{
  MyStruct s;
  std::tie(s.i, s.b, s.d) = src;
  return s;
}

我知道琐碎的内存复制可以做到这一点,但它依赖于对齐和实现?

您提到"琐碎的内存复制"会起作用——只用于复制单个成员。所以基本上,从整个结构的memcpytuple,反之亦然,不会总是像你预期的那样(如果有的话);CCD_ 31的存储器布局不是标准化的。如果它确实有效,那么它在很大程度上取决于实现。

元组到struct的转换很简单,但我认为在当前的C++级别上,向后转换是不可能的。

#include <type_traits>
#include <utility>
#include <tuple>
namespace details
{
template< typename result_type, typename ...types, std::size_t ...indices >
result_type
make_struct(std::tuple< types... > t, std::index_sequence< indices... >) // &, &&, const && etc.
{
    return {std::get< indices >(t)...};
}
}
template< typename result_type, typename ...types >
result_type
make_struct(std::tuple< types... > t) // &, &&, const && etc.
{
    return details::make_struct< result_type, types... >(t, std::index_sequence_for< types... >{}); // if there is repeated types, then the change for using std::index_sequence_for is trivial
}
#include <cassert>
#include <cstdlib>
int main()
{
    using S = struct { int a; char b; double c; };
    auto s = make_struct< S >(std::make_tuple(1, '2', 3.0));
    assert(s.a == 1);
    assert(s.b == '2');
    assert(s.c == 3.0);
    return EXIT_SUCCESS;
}

活生生的例子。