Boost.Hana: boost::hana::range< T, from, to > Struct Template Reference

| Boost.Hana 1.0.0 Your standard library for metaprogramming |

Synopsis of associated functions | Friends | Public Member Functions | List of all members
boost::hana::range< T, from, to > Struct Template Reference
Data types

## Description

### template<typename T, T from, T to>
struct boost::hana::range< T, from, to >

Compile-time half-open interval of `hana::integral_constant`s.

A `range` represents a half-open interval of the form `[from, to)` containing `hana::integral_constant`s of a given type. The `[from, to)` notation represents the values starting at `from` (inclusively) up to but excluding `from`. In other words, it is a bit like the list `from, from+1, ..., to-1`.

In particular, note that the bounds of the range can be any `hana::integral_constant`s (negative numbers are allowed) and the range does not have to start at zero. The only requirement is that `from <= to`.

Note
  The representation of `hana::range` is implementation defined. In particular, one should not take for granted the number and types of template parameters. The proper way to create a `hana::range` is to use `hana::range_c` or `hana::make_range`.

## Modeled concepts

1. `Comparable`
   Two ranges are equal if and only if they are both empty or they both span the same interval.
   // Copyright Louis Dionne 2013-2016
   // Distributed under the Boost Software License, Version 1.0.
   // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
   #include <boost/hana/assert.hpp>
   #include <boost/hana/equal.hpp>
   #include <boost/hana/integral_constant.hpp>
   #include <boost/hana/not_equal.hpp>
   #include <boost/hana/range.hpp>
   namespace hana = boost::hana;
   // empty ranges are equal
   BOOST_HANA_CONSTANT_CHECK(hana::make_range(hana::int_c<6>, hana::int_c<6>) == hana::make_range(hana::int_c<0>, hana::int_c<0>));
   // otherwise, ranges are equal if and only if they span the same interval
   BOOST_HANA_CONSTANT_CHECK(hana::make_range(hana::int_c<2>, hana::int_c<5>) == hana::make_range(hana::int_c<2>, hana::int_c<5>));
   BOOST_HANA_CONSTANT_CHECK(hana::make_range(hana::int_c<0>, hana::int_c<3>) != hana::make_range(hana::int_c<-1>, hana::int_c<3>));
   int main() { }
2. `Foldable`
   Folding a `range` is equivalent to folding a list of the `integral_constant`s in the interval it spans.
   // Copyright Louis Dionne 2013-2016
   // Distributed under the Boost Software License, Version 1.0.
   // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
   #include <boost/hana/assert.hpp>
   #include <boost/hana/equal.hpp>
   #include <boost/hana/fold_left.hpp>
   #include <boost/hana/integral_constant.hpp>
   #include <boost/hana/plus.hpp>
   #include <boost/hana/range.hpp>
   #include <boost/hana/tuple.hpp>
   #include <boost/hana/unpack.hpp>
   namespace hana = boost::hana;
   BOOST_HANA_CONSTANT_CHECK(
   hana::fold_left(hana::make_range(hana::int_c<0>, hana::int_c<4>), hana::int_c<0>, hana::plus) == hana::int_c<6>
   );
   BOOST_HANA_CONSTANT_CHECK(
   hana::unpack(hana::make_range(hana::int_c<-2>, hana::int_c<2>), hana::make_tuple) ==
   hana::make_tuple(hana::int_c<-2>, hana::int_c<-1>, hana::int_c<0>, hana::int_c<1>)
   );
   int main() { }
3. `Iterable`
   Iterating over a `range` is equivalent to iterating over a list of the values it spans. In other words, iterating over the range `[from, to)` is equivalent to iterating over a list containing `from, from+1, from+2, ..., to-1`. Also note that `operator[]` can be used in place of the `at` function.
   // Copyright Louis Dionne 2013-2016
   // Distributed under the Boost Software License, Version 1.0.
   // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
   #include <boost/hana/assert.hpp>
   #include <boost/hana/back.hpp>
   #include <boost/hana/drop_front.hpp>
   #include <boost/hana/equal.hpp>
   #include <boost/hana/front.hpp>
   #include <boost/hana/integral_constant.hpp>
   #include <boost/hana/is_empty.hpp>
   #include <boost/hana/not.hpp>
   #include <boost/hana/range.hpp>
   namespace hana = boost::hana;
   constexpr auto r = hana::make_range(hana::int_c<0>, hana::int_c<1000>);
   BOOST_HANA_CONSTANT_CHECK(hana::front(r) == hana::int_c<0>);
   BOOST_HANA_CONSTANT_CHECK(hana::back(r) == hana::int_c<999>);
   BOOST_HANA_CONSTANT_CHECK(hana::drop_front(r) == hana::make_range(hana::int_c<1>, hana::int_c<1000>));
   BOOST_HANA_CONSTANT_CHECK(!hana::is_empty(r));
   BOOST_HANA_CONSTANT_CHECK(hana::is_empty(hana::make_range(hana::int_c<3>, hana::int_c<3>)));
   int main() { }
4. `Searchable`
   Searching a `range` is equivalent to searching a list of the values in the range `[from, to)`, but it is much more compile-time efficient.
   // Copyright Louis Dionne 2013-2016
   // Distributed under the Boost Software License, Version 1.0.
   // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
   #include <boost/hana/assert.hpp>
   #include <boost/hana/equal.hpp>
   #include <boost/hana/find.hpp>
   #include <boost/hana/integral_constant.hpp>
   #include <boost/hana/optional.hpp>
   #include <boost/hana/range.hpp>
   namespace hana = boost::hana;
   BOOST_HANA_CONSTANT_CHECK(hana::find(hana::make_range(hana::int_c<1>, hana::int_c<25>), hana::int_c<10>) == hana::just(hana::int_c<10>));
   BOOST_HANA_CONSTANT_CHECK(hana::find(hana::make_range(hana::int_c<1>, hana::int_c<25>), hana::int_c<200>) == hana::nothing);
   int main() { }

## Synopsis of associated functions
template<>
constexpr auto | make< range_tag >
| Create a `hana::range` representing a half-open interval of `integral_constant`s. More...
constexpr auto | make_range = make<range_tag>
| Alias to `make<range_tag>`; provided for convenience. More...
template<typename T , T from, T to>
constexpr auto | range_c = make_range(integral_c<T, from>, integral_c<T, to>)
| Shorthand to create a `hana::range` with the given bounds. More...

## Friends
template<typename X , typename Y >
constexpr auto | operator== (X &&x, Y &&y)
| Equivalent to `hana::equal`
template<typename X , typename Y >
constexpr auto | operator!= (X &&x, Y &&y)
| Equivalent to `hana::not_equal`

## Public Member Functions
template<typename N >
decltype(auto) constexpr | operator[] (N &&n)
| Equivalent to `hana::at`

## Associated functions

template<typename T , T from, T to>
template<>

constexpr auto make< range_tag >
| related

Initial value:
= [](auto const& from, auto const& to) {
return range<implementation_defined>{implementation_defined};
}

Create a `hana::range` representing a half-open interval of `integral_constant`s.

Given two `IntegralConstant`s `from` and `to`, `make<range_tag>` returns a `hana::range` representing the half-open interval of `integral_constant`s `[from, to)`. `from` and `to` must form a valid interval, which means that `from <= to` must be true. Otherwise, a compilation error is triggered. Also note that if `from` and `to` are `IntegralConstant`s with different underlying integral types, the created range contains `integral_constant`s whose underlying type is their common type.

## Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#include <boost/hana/assert.hpp>
#include <boost/hana/core/make.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/range.hpp>
namespace hana = boost::hana;
constexpr auto irange = hana::make<hana::range_tag>(hana::int_c<0>, hana::int_c<10>); // [0, 10) int
BOOST_HANA_CONSTANT_CHECK(irange == hana::make<hana::range_tag>(hana::int_c<0>, hana::int_c<10>));
constexpr auto lrange = hana::make<hana::range_tag>(hana::int_c<0>, hana::long_c<10>); // [0, 10) long
BOOST_HANA_CONSTANT_CHECK(lrange == hana::make<hana::range_tag>(hana::long_c<0>, hana::long_c<10>));
int main() { }
template<typename T , T from, T to>

constexpr auto make_range = make<range_tag>
| related

Alias to `make<range_tag>`; provided for convenience.

template<typename T , T from, T to>
template<typename T , T from, T to>

constexpr auto range_c = make_range(integral_c<T, from>, integral_c<T, to>)
| related

Shorthand to create a `hana::range` with the given bounds.

This shorthand is provided for convenience only and it is equivalent to `make_range`. Specifically, `range_c<T, from, to>` is such that

range_c<T, from, to> == make_range(integral_c<T, from>, integral_c<T, to>)

Template Parameters

  T | The underlying integral type of the `integral_constant`s in the created range.
  from | The inclusive lower bound of the created range.
  to | The exclusive upper bound of the created range.

## Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#include <boost/hana/assert.hpp>
#include <boost/hana/back.hpp>
#include <boost/hana/drop_front.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/front.hpp>
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/range.hpp>
namespace hana = boost::hana;
BOOST_HANA_CONSTANT_CHECK(hana::front(hana::range_c<int, 0, 5>) == hana::int_c<0>);
BOOST_HANA_CONSTANT_CHECK(hana::back(hana::range_c<unsigned long, 0, 5>) == hana::ulong_c<4>);
BOOST_HANA_CONSTANT_CHECK(hana::drop_front(hana::range_c<int, 0, 5>) == hana::make_range(hana::int_c<1>, hana::int_c<5>));
int main() { }