将integral_constants元组转换为整数的constexpr元组

时间:2018-03-09 13:47:49

标签: c++ lambda c++14 constexpr boost-hana

假设我有hana::integral_constant这样的元组:

auto tuple_of_int_const = hana::make_tuple(hana::integral_constant<int,0>(),
                                           hana::integral_constant<int,1>());

我现在试图从这里产生一个constexpr元组,如下所示:

constexpr auto constexpr_tuple = hana::make_tuple(0,1);

我的尝试:

#include "boost/hana.hpp"


using namespace boost;

auto tuple_of_int_const = hana::make_tuple(hana::integral_constant<int,0>(),
                                           hana::integral_constant<int,1>());

constexpr auto tuple = hana::transform(tuple_of_int_const,[](auto x){ return x(); });

问题是lambda不是constexpr .......所以问题是: 如何将integral_constant的元组转换为constexpr lambda?

注意:

我正在使用c ++ 14

2 个答案:

答案 0 :(得分:2)

Boost.Hana将函数value_of作为Constant概念的一部分。

#include <boost/hana.hpp>

namespace hana = boost::hana;

int main() {
  auto tuple_of_int_const = hana::make_tuple(hana::integral_constant<int,0>(),
                                             hana::integral_constant<int,1>());

  constexpr auto tuple = hana::transform(tuple_of_int_const, hana::value_of);

  static_assert(hana::make_tuple(0, 1) == tuple, "");
}

答案 1 :(得分:1)

您可以创建常规模板功能:

template <typename ... Ts>
constexpr auto tuple_transform(std::tuple<Ts...>)
{
    return std::make_tuple(Ts{}()...);
}

Demo