项目作者: frengels

项目描述 :
Heterogeneous ranges for c++20
高级语言: C++
项目地址: git://github.com/frengels/hera.git
创建时间: 2019-08-19T03:42:46Z
项目社区:https://github.com/frengels/hera

开源协议:MIT License

下载


Heterogeneous ranges

Goals

Ease of use

With ranges coming to c++20 it is the goal of this library to use provide a syntax as close as possible to the originals whilst working with the limitations of iterating heterogeneous views.

Lazy

Unlike in boost.hana, views are constructed lazily. There are not potential unnecessary copies taking place. Whilst this property is not important for hana’s main usecase of operating on types, it can be for many applications.

Example

Assume you have a tuple of objects and for some reason you’re only interested in integers (or any other constant condition).

  1. // we only care about the integers in this tuple
  2. auto tup = std::make_tuple("Hello hera!", 50, "Don't care about me", 50);
  3. auto tup_view = hera::tuple_view{tup};
  4. // Predicate must return an integral constant of type bool.
  5. auto only_ints = tup_view | hera::views::filter([](auto x) {
  6. return std::is_same<decltype(x), int>{};
  7. });
  8. auto res = 0;
  9. hera::for_each(only_ints, [&](auto filtered) { return res += filtered; });
  10. std::cout << "result is: " << res << '\n';
  11. // now we only care about the strings
  12. auto only_str = tup_view | hera::views::filter([](auto str) {
  13. return std::is_same<decltype(str), const char*>{};
  14. });
  15. hera::for_each(only_str, [](auto str) { std::cout << str << '|'; });
  16. std::cout << '\n';

The result of the filter must return an integral constant, otherwise the compiler cannot know the size of the range. As a result filter_view does not store anything other than the underlying view (not even the predicate), making filtering a zero overhead.

Available views

  • array
  • tuple
  • filter
  • transform
  • head
  • iota
  • move
  • ref
  • drop
  • reorder
  • zip
  • enumerate

Available algorithms

  • find_if
  • for_each
  • unpack
  • any_of
  • all_of
  • sort