boost::hana 用于可变模板实例化的元组解包
boost::hana tuple unpacking for variadic template instantiation
与 相关 我想知道是否可以使用 boost::hana:
以直接的方式实现这样的事情
#include <boost/hana.hpp>
#include <boost/hana/unpack.hpp>
namespace hana = boost::hana;
template<typename ... T>
struct A {};
int main() {
auto my_tuple = hana::tuple_t<int, double, float>;
// Is there any way to unpack my_tuple as template arguments for A?
// Something like
using MyStruct = A<hana::unpack_types(my_tuple)...>;
static_assert(std::is_same<MyStruct, A<int, double, float>>::value, "Ooops!");
}
你可以自己做:
template <template <typename...> class C, typename Tuple> struct RebindImpl;
template <template <typename...> class C, typename ... Ts>
struct RebindImpl<C, hana::tuple_t<Ts...>>{
using type = C<Ts...>;
};
template <template <typename...> class C, typename Tuple>
using Rebind = typename RebindImpl<C, Tuple>::type;
如果您只关心获得正确的类型,那么一个简单的方法可以做到这一点:
template <typename... Args>
constexpr A<Args...> make_A(hana::tuple_t<Args...>) {
return {};
}
使用template_
to lift A
into a metafunction, then call unpack
:
using MyStruct = decltype(hana::unpack(my_tuple, hana::template_<A>))::type;
与
#include <boost/hana.hpp>
#include <boost/hana/unpack.hpp>
namespace hana = boost::hana;
template<typename ... T>
struct A {};
int main() {
auto my_tuple = hana::tuple_t<int, double, float>;
// Is there any way to unpack my_tuple as template arguments for A?
// Something like
using MyStruct = A<hana::unpack_types(my_tuple)...>;
static_assert(std::is_same<MyStruct, A<int, double, float>>::value, "Ooops!");
}
你可以自己做:
template <template <typename...> class C, typename Tuple> struct RebindImpl;
template <template <typename...> class C, typename ... Ts>
struct RebindImpl<C, hana::tuple_t<Ts...>>{
using type = C<Ts...>;
};
template <template <typename...> class C, typename Tuple>
using Rebind = typename RebindImpl<C, Tuple>::type;
如果您只关心获得正确的类型,那么一个简单的方法可以做到这一点:
template <typename... Args>
constexpr A<Args...> make_A(hana::tuple_t<Args...>) {
return {};
}
使用template_
to lift A
into a metafunction, then call unpack
:
using MyStruct = decltype(hana::unpack(my_tuple, hana::template_<A>))::type;