1

假设我有一个hana::integral_constant像这样的 s 元组:

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_constants 的元组转换为 constexpr lambda?

笔记:

我正在使用 c++14

4

2 回答 2

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, "");
}
于 2018-03-09T16:06:37.990 回答
1

您可以创建常规模板函数:

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

演示

于 2018-03-09T14:04:27.860 回答