Boost.Hana  1.7.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.
 
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 friend auto operator== (X &&x, Y &&y)
 Equivalent to hana::equal
 
template<typename X , typename Y >
constexpr friend auto operator!= (X &&x, Y &&y)
 Equivalent to hana::not_equal
 

Public Member Functions

template<typename N >
constexpr decltype(auto) 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() { }

◆ 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() { }
equal.hpp
Defines boost::hana::equal.
boost::hana::is_empty
constexpr auto is_empty
Returns whether the iterable is empty.
Definition: is_empty.hpp:33
drop_front.hpp
Defines boost::hana::drop_front.
BOOST_HANA_CONSTANT_CHECK
#define BOOST_HANA_CONSTANT_CHECK(...)
Equivalent to BOOST_HANA_CONSTANT_ASSERT, but not influenced by the BOOST_HANA_CONFIG_DISABLE_ASSERTI...
Definition: assert.hpp:239
tuple.hpp
Defines boost::hana::tuple.
integral_constant.hpp
Defines boost::hana::integral_constant.
make.hpp
Defines boost::hana::make.
boost::hana::find
constexpr auto find
Finds the value associated to the given key in a structure.
Definition: find.hpp:44
boost::hana::front
constexpr auto front
Returns the first element of a non-empty iterable.
Definition: front.hpp:32
find.hpp
Defines boost::hana::find.
back.hpp
Defines boost::hana::back.
boost::hana::plus
constexpr auto plus
Associative binary operation on a Monoid.
Definition: plus.hpp:47
plus.hpp
Defines boost::hana::plus.
range.hpp
Defines boost::hana::range.
boost::hana
Namespace containing everything in the library.
Definition: accessors.hpp:20
boost::hana::to
constexpr auto to
Converts an object from one data type to another.
Definition: to.hpp:97
boost::hana::unpack
constexpr auto unpack
Invoke a function with the elements of a Foldable as arguments.
Definition: unpack.hpp:79
assert.hpp
Defines macros to perform different kinds of assertions.
fold_left.hpp
Defines boost::hana::fold_left.
boost::hana::drop_front
constexpr auto drop_front
Drop the first n elements of an iterable, and return the rest.
Definition: drop_front.hpp:47
not_equal.hpp
Defines boost::hana::not_equal.
is_empty.hpp
Defines boost::hana::is_empty.
front.hpp
Defines boost::hana::front.
not.hpp
Defines boost::hana::not_.
boost::hana::back
constexpr auto back
Returns the last element of a non-empty and finite iterable.
Definition: back.hpp:32
unpack.hpp
Defines boost::hana::unpack.
boost::hana::range::make_range
constexpr auto make_range
Alias to make<range_tag>; provided for convenience.
Definition: range.hpp:115
optional.hpp
Defines boost::hana::optional.