A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove

constant

A helper class for templates that require constants and classes that declare value and value_type members.

The class allows constants to be wrapped in a template that defines value and value_type.
This can be particularly useful in recursively defined templates where the value is defined as either a constant or a
further invocation of the template.

Example

Take the example of etl::sqrt.

template <const intmax_t VALUE, const intmax_t I = 1>
struct sqrt
{
  typedef typename etl::conditional<((I * I) < VALUE),
                                    etl::sqrt<VALUE, I + 1>,
                                    etl::constant<intmax_t, I> >::type type;

  enum value_type
  {
    value = type::value
  };
};

// Stop condition.
template <const intmax_t I>
struct sqrt<I, I>
{
  enum value_type
  {
    value = I
  };
};

etl::conditional will select one of the two types based on the boolean template parameter. The types are expected
to have a value member. This is achieved for the constant by wrapping it in etl::constant.
constant.h