C++ 如何使用Boost Hana删除元编程递归
我正在尝试根据发送到函数的类型创建位集。但是让我们稍微减少一下测试用例 警告:在本例中,我使用的是自动gcc扩展,不需要使用模板参数C++ 如何使用Boost Hana删除元编程递归,c++,lambda,metaprogramming,boost-hana,C++,Lambda,Metaprogramming,Boost Hana,我正在尝试根据发送到函数的类型创建位集。但是让我们稍微减少一下测试用例 警告:在本例中,我使用的是自动gcc扩展,不需要使用模板参数 namespace-hana=boost::hana; constexpr decltype(auto)rec(auto i、auto max、auto f、auto returnValue){ 返回值|=f(i); if constexpr(i
namespace-hana=boost::hana;
constexpr decltype(auto)rec(auto i、auto max、auto f、auto returnValue){
返回值|=f(i);
if constexpr(i return 0错误是由使用手动递归导致的越界访问引起的。函数式编程的部分目的是提供结构以消除此类错误的可能性
这里有几个例子,但建议您看看手册中的概念,因为它是使用Boost.Hana的基础
为您隐藏递归,并可以通过快速跟踪减少递归调用的数量:
constexpr decltype(auto) foo = [](auto ct, auto ... type) {
constexpr auto tuple = hana::make_tuple(type...);
return hana::fold_left(hana::make_range(hana::size_c<0>, hana::size_c<3>), 0L,
[tuple, ct](auto returnValue, auto i)
{
// returnValue param is not constexpr
if (hana::contains(tuple, ct[i])) {
return returnValue | (1 << decltype(i)::value);
}
else
{
return returnValue;
}
}
);
};
i不在ct范围内。你检查iboost hana
标记问题。
constexpr decltype(auto) foo = [](auto ct, auto ... type) {
constexpr auto tuple = hana::make_tuple(type...);
return hana::fold_left(hana::make_range(hana::size_c<0>, hana::size_c<3>), 0L,
[tuple, ct](auto returnValue, auto i)
{
// returnValue param is not constexpr
if (hana::contains(tuple, ct[i])) {
return returnValue | (1 << decltype(i)::value);
}
else
{
return returnValue;
}
}
);
};
constexpr decltype(auto) foo = [](auto ct, auto ... type) {
constexpr auto tuple = hana::make_tuple(type...);
auto f = [tuple, ct](auto i)
{
return hana::contains(tuple, ct[i]) ? (1 << decltype(i)::value) : 0;
};
return hana::unpack(hana::make_range(hana::size_c<0>, hana::size_c<3>),
[f](auto ...i) { return (f(i) | ...); }
);
};