【发布时间】:2018-10-05 14:10:42
【问题描述】:
我需要将boost::tuple 转换为对应的boost::fusion::tuple。我已经弄清楚了对应的类型。
但我希望有一个内置函数可以做到这一点。我真的不想重新发明这些东西。我在 boost fusion 文档中进行了搜索,但没有找到任何内容。
【问题讨论】:
标签: c++ boost tuples boost-fusion
我需要将boost::tuple 转换为对应的boost::fusion::tuple。我已经弄清楚了对应的类型。
但我希望有一个内置函数可以做到这一点。我真的不想重新发明这些东西。我在 boost fusion 文档中进行了搜索,但没有找到任何内容。
【问题讨论】:
标签: c++ boost tuples boost-fusion
你可能会使用类似的东西:
template <class Tuple>
auto to_fusion(Tuple&& tuple)
{
std::apply(
[](auto&&... args){
return boost::fusion::make_tuple(decltype(args)(args)...);
},
std::forward<Tuple>(tuple));
}
【讨论】:
c++14版本:
template<std::size_t...Is, class T>
auto to_fusion( std::index_sequence<Is...>, T&& in ) {
using std::get;
return boost::fusion::make_tuple( get<Is>(std::forward<T>(in))... );
}
template<class...Ts>
auto to_fusion( boost::tuple<Ts...> in ) {
return to_fusion( std::make_index_sequence<::boost::tuples::length< boost::tuple<Ts...>>::value>{}, std::move(in) );
}
template<class...Ts>
boost::fusion::tuple<Ts...> to_fusion( std::tuple<Ts...> in ) {
return to_fusion( std::make_index_sequence<sizeof...(Ts)>{}, std::move(in) );
}
我不知道有内置版本。
在c++11 中添加尾随-> decltype(boost::fusion::make_tuple( get<Is>(std::forward<T>(in))... ))。你还需要make_index_sequence,它可能有一个 boost 等价物。
【讨论】:
Ts... 是Is... 吗?即使我将Ts 更改为Is,我也得到type/value 不匹配
get。我只是引入了一个“已知”的模板获取,因此在 ADL 之前编译器将其视为模板。
/usr/include/boost/tuple/detail/tuple_basic.hpp:133:33: error: no type named ‘head_type’ in ‘boost::tuples::detail::drop_front<2>::apply<boost::tuples::cons<point_2d<double>, boost::tuples::cons<point_2d<double>, boost::tuples::null_type> > >::type’ {aka ‘struct boost::tuples::null_type’}
make_index_sequence<sizeof...(Ts)> -> index_sequence_for<Ts...>?