Boost.Hana  1.6.0
Your standard library for metaprogramming
boost::hana::range< T, from, to > Struct Template Reference

Description

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

Compile-time half-open interval of hana::integral_constants.

A range represents a half-open interval of the form [from, to) containing hana::integral_constants 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_constants (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. More details in the tutorial.

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-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    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_constants in the interval it spans.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    namespace hana = boost::hana;
    hana::fold_left(hana::make_range(hana::int_c<0>, hana::int_c<4>), hana::int_c<0>, hana::plus) == hana::int_c<6>
    );
    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-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    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(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-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    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_constants. 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

◆ make< range_tag >

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_constants.

Given two IntegralConstants from and to, make<range_tag> returns a hana::range representing the half-open interval of integral_constants [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 IntegralConstants with different underlying integral types, the created range contains integral_constants whose underlying type is their common type.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
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() { }

◆ make_range

template<typename T , T from, T to>
constexpr auto make_range = make<range_tag>
related

Alias to make<range_tag>; provided for convenience.

◆ range_c

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
TThe underlying integral type of the integral_constants in the created range.
fromThe inclusive lower bound of the created range.
toThe exclusive upper bound of the created range.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
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() { }