boost-hana

Boost hana get index of first matching

痴心易碎 提交于 2019-12-20 02:46:08
问题 So I am trying to make a library using boost::hana that requires the functionality to get the index of a element based on the value: constexpr auto tup = boost::hana::make_tuple(3_c, boost::hana::type_c<bool>); auto index = get_index_of_first_matching(tup, boost::hana::type_c<bool>); // ^^^^^ would be a boost::hana::int_<1> Is there a possible way to do this? Better yet, is it already in hana and I don't know about it? Thanks for the support! 回答1: Hana does not provide an algorithm to do this

Change runtime research for a compile time one

笑着哭i 提交于 2019-12-11 06:14:32
问题 I'm trying to implement a generic ECS library in C++ for learning purpose. I was thinking about a lot of way to implement things but I always run into a problem. So if you could help me with this one : Let say I have a constexpr hana::tuple of hana::type_c Components, something like : struct C1 {}; struct C2 {}; struct C3 {}; constexpr auto components = hana::to_tuple(hana::tuple_t<C1, C2, C3>); And now I have a component storage type, which is not a problem here, so let's call it Storage

How to remove metaprogramming recursion with Boost Hana

↘锁芯ラ 提交于 2019-12-10 22:08:58
问题 I'm trying to create a bitset according to the type send to the function. But let's reduce the test case a little. Warning : I'm using auto gcc extension for this example, I don't need to use template parameter. namespace hana = boost::hana; constexpr decltype(auto) rec(auto i, auto max, auto f, auto returnValue) { return returnValue |= f(i); if constexpr (i < max) //"infinite" loop if no constexpr return rec(i + hana::size_c<1>, max, f, returnValue); else return returnValue; } constexpr

Clang compile error related to static_assert and boost::hana

孤者浪人 提交于 2019-12-10 13:45:28
问题 Consider the following problem which compiles successfully on Clang 3.8 using -std=c++14 . #include <boost/hana.hpp> namespace hana = boost::hana; int main() { constexpr auto indices = hana::range<unsigned, 0, 3>(); hana::for_each(indices, [&](auto i) { hana::for_each(indices, [&](auto j) { constexpr bool test = (i == (j == i ? j : i)); static_assert(test, "error"); }); }); } The test is quite non-sensical but that is not the point. Consider now an alternative version where the test is

Nested loops unrolling using metaprogramming

左心房为你撑大大i 提交于 2019-12-08 16:05:25
问题 I have a number of nested loops with small sizes I, J, ... known at compile time, e.g. for(int i = 0; i < I; ++i) { for(int j = 0; j < J; ++j) { // ... // do sth with (i,j,...) } } I need to unroll the loops using the sizes I, J, ... in such a way that I can use each coordinate combination at compile time . To clarify, consider the following structure and take 2 nested loops with sizes I = 2, J = 3 . template<int... I> struct C { static void f() { // do sth } }; I can not use the indices i, j

C++: Expand array elements as parameter of a function using boost::hana

混江龙づ霸主 提交于 2019-12-07 16:19:41
问题 I discovered two months ago boost::hana. Seems very powerfull so I decided to take a look. From the documentation I saw this examples: std::string s; hana::int_c<10>.times([&]{ s += "x"; }); that is equivalent to: s += "x"; s += "x"; ... s += "x"; // 10 times I'd like to know if it is possible (and if yes how) to write smthg like: std::string s; std::array<int, 10> xs = {1, 3, 5, ...}; hana::int_c<10>.times([&](int i){ s += std::to_string(xs[i]) + ","; }); a sort of "unpacking" at compile

C++14 Metaprogramming: Automagically build a list of types at compile / init time

不问归期 提交于 2019-12-07 04:11:40
问题 Using C++14 and some combination of the Curiously Recurring Template Pattern (CRTP) and possibly Boost.Hana (or boost::mpl if you wish), can I build a list of types at compile time (or static initialization time) without an explicit declaration? As an example, I have something like this (see it on Coliru ): #include <iostream> #include <boost/hana/tuple.hpp> #include <boost/hana/for_each.hpp> namespace { struct D1 { static constexpr auto val = 10; }; struct D2 { static constexpr auto val = 20

Is there a way to obtain the map type?

此生再无相见时 提交于 2019-12-06 16:35:56
I have the following code: auto myMap = hana::make_map( hana::make_pair(hana::type_c<int>, 2), hana::make_pair(hana::type_c<char const*>, "hi"), hana::make_pair(hana::type_c<double>, 3.0) ); Is there a way to know the type of 'myMap' beforehand? I try it with: using MyMap = hana::map<hana::pair<hana::type<int>, int>, ...>; but it fails because decltype(myMap) is hana::map< implementation-defined >. Is there a kind of 'result_of' metafunction that would give the imp-defined type? Like: using MyMap = typename hana::result_of_map<hana::pair<hana::type<int>, int>, ...>::type; I need the type to

Boost.Hana: How to check if function has specialisation for a certain type?

依然范特西╮ 提交于 2019-12-06 12:23:06
问题 I have a template function that has no definition by default but it specialised by some types: template <typename T> auto foo(bar &, const T &) -> void; template <> auto foo<std::string>(bar &, const std::string &) -> void {} How do I write a constexpr function that tells me if type T has a specialisation for the above function? My best effort: namespace detail { auto has_foo(hana::is_valid([](auto &b, const auto &t) -> decltype(foo(b, t)) {})); } // namespace detail template <typename T>

C++14 Metaprogramming: Automagically build a list of types at compile / init time

主宰稳场 提交于 2019-12-05 06:11:51
Using C++14 and some combination of the Curiously Recurring Template Pattern (CRTP) and possibly Boost.Hana (or boost::mpl if you wish), can I build a list of types at compile time (or static initialization time) without an explicit declaration? As an example, I have something like this (see it on Coliru ): #include <iostream> #include <boost/hana/tuple.hpp> #include <boost/hana/for_each.hpp> namespace { struct D1 { static constexpr auto val = 10; }; struct D2 { static constexpr auto val = 20; }; struct D3 { static constexpr auto val = 30; }; } int main() { // How to avoid explicitly defining