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

Utility


A few useful utility functions and classes.
____________________________________________________________________________________________________
pair
template <typename T1, typename T2>
struct pair
A clone of std::pair

C++03
template <typename T1, typename T2>
pair<T1, T2> make_pair(T1 a, T2 b)

C++11 and above
template <typename T1, typename T2>
pair<T1, T2> make_pair(T1&& a, T2&& b)
Returns a pair.
____________________________________________________________________________________________________
template <size_t Index, typename T1, typename T2>
struct tuple_element<Index, ETL_OR_STD::pair<T1, T2>>
Specialisation for pair.
Gets the type in the pair at Index.
Static asserts if Index is not 0 or 1.
20.40.1

template <typename T1, typename T2>
struct tuple_size<ETL_OR_STD::pair<T1, T2>>
Specialisation for pair.
Gets the size of the pair, which is always 2.
20.40.1
____________________________________________________________________________________________________
Template deduction guides
C++17 and above

template <typename T1, typename T2>
pair(T1, T2) ->pair<T1, T2>;
____________________________________________________________________________________________________
exchange

template <typename T, typename U = T>
T exchange(T& object, const U& new_value)
Copies the new value to object and returns the old value.
Note: This is not an atomic operation.
__________________________________________________________________________________________________
add_const

template <typename T>
typename etl::add_const<T>::type& as_const(T& t)
Returns a value of type T as a const T.
__________________________________________________________________________________________________

coordinate_2d


template <typename T>
struct coordinate_2d

Member types

T x;
T y;
__________________________________________________________________________________________________

In_place disambiguation tags.


struct in_place_t

inline constexpr in_place_t in_place{}; // C++17
____________________________________________________________________________________________________
template <typename T> struct in_place_type_t

template <typename T>
inline constexpr in_place_type_t<T> in_place_type{}; // C++17
____________________________________________________________________________________________________
template <size_t I> struct in_place_index_t

template <size_t I>
inline constexpr in_place_index_t<I> in_place_index{}; // C++17
__________________________________________________________________________________________________

declval


template <typename T>
typename etl::add_rvalue_reference<T>::type declval() ETL_NOEXCEPT;
C++11
__________________________________________________________________________________________________
functor
For C++11 and above.
20.27.0

template <typename TReturn, typename... TParams>
class functor
Wraps a free/global function in a functor.
____________________________________________________________________________________________________
constexpr functor(TReturn(*ptr_)(TParams...))
Constructs a functor from a function pointer.
____________________________________________________________________________________________________
constexpr TReturn operator()(TParams... args) const
Function operator.
Calls the wrapped function with the forwarded parameters.
____________________________________________________________________________________________________

Example


void int Function(int i)
{
  return i;
}

// Note that the functor deduces the template parameters.
constexpr etl::functor f(Function);
__________________________________________________________________________________________________
member_function_wrapper
For C++11 and above.
20.27.0

template <typename TReturn, typename... TParams>
class member_function_wrapper<TReturn(TParams...)>
Wraps a member function in a static member function.
____________________________________________________________________________________________________
template <typename T, T& Instance, TReturn(T::* Method)(TParams...)>
static constexpr TReturn function(TParams... params)
The static function that calls the member function.
____________________________________________________________________________________________________

Example


class MyClass
{
public:

  int MemberFunction(int i)
  {
    return 2 * i;
  }
};

MyClass test;

constexpr int(*pf)(int)
        = &etl::member_function_wrapper<int(int)>::function<MyClass, test, &MyClass::MemberFunction>;

// Call
int result = pf(1);
__________________________________________________________________________________________________
functor_wrapper
For C++11 and above.
20.27.0

template <typename TReturn, typename... TParams>
class functor_wrapper<TReturn(TParams...)>
Wraps a functor in a static member function.
____________________________________________________________________________________________________
template <typename TFunctor, TFunctor& Instance>
static constexpr TReturn function(TParams... params)
The static function that calls the member function.
____________________________________________________________________________________________________

Example


class MyClass
{
public:

  int operator()(int i)
  {
    return 2 * i;
  }
};

MyClass test;

constexpr int(*pf)(int) = &etl::functor_wrapper<int(int)>::function<MyClass, test>;

// Call
int result = pf(1);
__________________________________________________________________________________________________
functor_as_static
For C++17 and above.
20.40.0

Wraps a functor with a static free function at compile time.
Creates a static member 'call' that calls the specified functor.

template <auto& Instance>
struct functor_as_static
____________________________________________________________________________________________________
template <typename... TArgs>
static constexpr auto call(TArgs&&... args)
Member static function that calls the functor
____________________________________________________________________________________________________

Example


struct Test
{
  int operator()(int i)
  {
    return 2 * i;
  }
};

Test test;

using fas_t = etl::functor_as_static<test>;
fas_t::call(1));
__________________________________________________________________________________________________
member_function_as_static
For C++17 and above.
20.40.0

Wraps a member function with a static free function at compile time.
Creates a static member function that calls the specified member function.

template <auto Method, auto& Instance>
struct member_function_as_static
____________________________________________________________________________________________________
template <typename... TArgs>
static constexpr auto call(TArgs&&... args)
Member static function that calls the member function.
____________________________________________________________________________________________________

Example


struct Test
{
  int Function()(int i)
  {
    return 2 * i;
  }
};

Test test;

using mfas_t = etl::functor_as_static<&Test::Function, test>;
mfas_t::call(1));

__________________________________________________________________________________________________
member_function_as_functor
For C++17 and above.
20.40.0

Wraps a member function with a functor at compile time.
Creates a functor that calls the specified member function.

template <auto Method, auto& Instance>
class member_function_as_functor

template <typename... TArgs>
constexpr auto operator()(TArgs&&... args) const
Calls the functor.
____________________________________________________________________________________________________

Example


struct Test
{
  int Function()(int i)
  {
    return 2 * i;
  }
};

constexpr etl::member_function_as_functor<&Test::Function, test> mfaf;
mfaf(1);
__________________________________________________________________________________________________
function_as_functor
For C++17 and above.
20.40.0

Wraps a function with a functor at compile time.
Creates a functor that calls the specified free function.

template <auto Function>
class function_as_functor
____________________________________________________________________________________________________
constexpr auto operator()(TArgs&&... args) const
Calls the functor.
____________________________________________________________________________________________________

Example


int Function()(int i)
{
  return 2 * i;
}

constexpr etl::function_as_functor<Function> faf;
faf(1);
__________________________________________________________________________________________________
function_ptr_as_functor
For C++11 and above.
20.40.0

Wraps a function pointer with a functor at run time.
Creates a functor that calls the specified free function.

template <typename TReturn, typename... TArgs>
class function_ptr_as_functor
____________________________________________________________________________________________________
constexpr function_ptr_as_functor(TReturn(*ptr_)(TArgs...))
Construct from a function pointer.
____________________________________________________________________________________________________
constexpr TReturn operator()(TArgs... args) const
Function operator.
____________________________________________________________________________________________________

Example


int Function()(int i)
{
  return 2 * i;
}

using function_type = decltype(Function);
constexpr function_type* fptr = Function;
constexpr etl::function_ptr_as_functor<function_type> fpaf(fptr)
__________________________________________________________________________________________________
integer_sequence
20.14.0

template <typename T, T... Integers>
class integer_sequence
__________________________________________________________________________________________________
index_sequence
20.14.0

template <size_t... Indices>
using index_sequence = etl::integer_sequence<size_t, Indices...>;

template <size_t N>
make_index_sequence
__________________________________________________________________________________________________

select1st

select2nd


select1st is a functor object that takes a single argument, a pair, and returns the pair::first element.
select2nd is a functor object that takes a single argument, a pair, and returns the pair::second element.
____________________________________________________________________________________________________

size_of_type

20.36.0


template <typename T>
ETL_CONSTEXPR size_t size_of_type()
Returns the size of the type defined in T.
____________________________________________________________________________________________________
template <typename T>
ETL_CONSTEXPR size_t size_of_type(const T&)
Returns the size of the type defined in T.
____________________________________________________________________________________________________
ETL_SIZE_OF_OBJECT_TYPE(Object, Type)
Returns the size of Type defined in the declared type of Object.
C++11 and above
20.36.0

ETL_SIZE_OF_CLASS_TYPE(Class, Type)
Returns the size of Type defined in Class.
20.36.0
____________________________________________________________________________________________________

nontype_t

20.43.0


Wraps a non-type template parameter as a type.
Defines a value associated with the template type.

If ETL_FORCE_CPP11_NONTYPE is defined then the C++14 and below variant is used.

C++14 and below

template <typename T, T Value>
struct nontype_t;

using FortyTwo = etl::nontype_t<int, 42>;

C++17 and above

template <auto Value>
struct nontype_t;

using FortyTwo = etl::nontype_t<42>