结构到std::元组的转换
struct to/from std::tuple conversion
假设我有相同类型布局的struct
和std::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::pair
、std::array
,以及任何您自定义的支持结构化绑定的代码(tuple_size
和get<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;
}
我知道琐碎的内存复制可以做到这一点,但它依赖于对齐和实现?
您提到"琐碎的内存复制"会起作用——只用于复制单个成员。所以基本上,从整个结构的memcpy
到tuple
,反之亦然,不会总是像你预期的那样(如果有的话);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;
}
活生生的例子。
- 将元组的向量转换/构造为堆
- 如何在编译时将齐次元组转换为数组?
- Boost.Hana:在 constexpr 上下文中将值元组转换为相应类型的元组
- 将多个数组转换为元组
- 将函数应用于元组中的每个元素,将每个元素强制转换为类型包中的不同类型,然后作为参数包传递
- 为什么此元组到引用元组 (std::tie) 转换有效?
- 将树转换为元组的编译时转换
- 如何将标准::数组转换为标准::元组?
- 如何将元组转换为C 11中的字节数组
- 元组尺寸默认构造函数及其转换为std :: size_t
- 使用 hana::transform 在 C++14 中转换元组内的类型
- 将STD ::变体转换为STD ::模板类实例的元组
- 将 integral_constants 元组转换为整数元组
- 错误:无法从"元组<[...]、std::__1::元组<无符号长长、无符号长长>>"到"常量元组<[...],uint_type>"的可行转换
- 将元组转换为变体
- Boost::P ython,将元组转换为Python有效,矢量<tuple>不
- 有没有一种方法可以将左值和右值的列表分别转换为具有引用类型和完整类型的元组
- 等价于元组的std::转换
- 将元组转换为函数参数
- 转换元组类型