Going from hana::tuple_t to hana::tuple

非 Y 不嫁゛ 提交于 2019-12-04 14:08:57

I believe what you really want here is something like

#include <boost/hana.hpp>
namespace hana = boost::hana;

constexpr auto types = hana::tuple_t<int, char, double, float>;
using Tuple = decltype(hana::unpack(types, hana::template_<hana::tuple>))::type;
// Tuple is hana::tuple<int, char, double, float>
// Now you can create such a tuple as you wish:
Tuple ts{1, 'x', 2.2, 3.4f};

Things like hana::template_ and hana::metafunction were built precisely to make this interoperation with types easy.

hana::tuple_t is just a template variable that is itself already a hana::tuple so converting to hana::tuple won't change anything.

template <typename ...T>
constexpr hana::tuple<hana::type<T>...> tuple_t{};

As mentioned in the comments, your call to hana::transform default initializes each member so you would expect values such as 0 for integral types.

Also, you are using BOOST_HANA_CONSTANT_ASSERT which checks compile-time values only. Raw int, char, double, and float values will not be constexpr.

BOOST_HANA_RUNTIME_ASSERT works for run-time values:

#include <boost/hana.hpp>

namespace hana = boost::hana;

constexpr auto types = hana::tuple_t<int, char, double, float>;

struct init_from_type_fn
{
  template <typename Type>
  constexpr auto operator()(Type) const
  {
    return typename Type::type{};
  }
};

constexpr init_from_type_fn init_from_type{};

int main()
{
  BOOST_HANA_RUNTIME_ASSERT(
    hana::equal(
      hana::transform(types, init_from_type),
      hana::make_tuple(0, '\0', 0.0, 0.0f)
    )
  );
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!