假设我有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
答案 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{}()...);
}