Skip to main content

type-traits.h File

C++ header file with declarations for the µTest++ type trait utilities and metaprogramming support. More...

Included Headers

#include "math.h"

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespacetype_traits

Type trait utilities and metaprogramming support for the µTest++ testing framework. More...

Classes Index

structfloating_point_constant<T, N, D, Size, P>

Struct template representing a generic floating point constant with custom size and precision. More...

structfunction_traits<R(*)(Args_T...)>

Struct template specialisation for extracting function traits from function pointer types. More...

structfunction_traits<R(Args_T...)>

Struct template specialisation for extracting function traits from plain function types. More...

structfunction_traits<R(T::*)(Args_T...) const>

Struct template specialisation for extracting function traits from const member function types. More...

structfunction_traits<R(T::*)(Args_T...)>

Struct template specialisation for extracting function traits from non-const member function types. More...

structgenuine_integral_value<T>

Struct template representing a genuine integral value. More...

structidentity<T, Extra>

Struct template for compile-time type identity. More...

structintegral_constant<N>

Struct template representing a generic integral constant. More...

structlist<Types>

Struct template representing a compile-time type list. More...

structop

Empty base struct for all operator types. More...

structrequires_<Cond>

Struct template for SFINAE requirements. More...

structrequires_<true>

Specialisation of the requirements struct template for true. More...

structvalue<T, Opt>

Struct template representing a generic value, accessible via a getter. More...

structvalue<T, type_traits::requires_t< type_traits::is_floating_point_v< T > >>

Struct template representing a floating point value with precision control. More...

Typedefs Index

template <bool Cond>
usingrequires_t = typename requires_< Cond >::type

Alias template for extracting the type member from requires_. More...

Functions Index

template <class T>
T &&declval (void)

Utility function template to simulate std::declval for type deduction. More...

template <class...>
constexpr autois_convertible (...)

Fallback function template for is_convertible, returns false if the conversion is not valid. More...

template <class From, class To>
constexpr auto is_convertible (int n) -> decltype(bool(To(declval< From >())))

Function template to determine if one type is convertible to another. More...

template <class... Ts>
constexpr boolis_valid (...)

Fallback function template for is_valid, returns false if the expression is not valid. More...

template <class... Ts, class Expr_T>
constexpr auto is_valid (Expr_T expr) -> decltype(expr(declval< Ts... >()), bool())

Variables Index

template <class T>
static constexpr autohas_epsilon_v = is_valid<T> ([] (auto t) -> decltype (void (t.epsilon)) {})

Variable template to determine if a type provides an epsilon member. More...

template <class T>
static constexpr autohas_npos_v = is_valid<T> ([] (auto t) -> decltype (void (t.npos)) {})

Variable template to determine if a type provides a static npos member. More...

template <class T>
static constexpr autohas_value_v = is_valid<T> ([] (auto t) -> decltype (void (t.value)) {})

Variable template to determine if a type provides a value member. More...

template <class T>
static constexpr autois_container_v = ...

Variable template to determine if a type models a container. More...

template <class From, class To>
constexpr autois_convertible_v = is_convertible<From, To> (0)

Variable template to determine if one type is convertible to another. More...

template <class T>
constexpr autois_floating_point_v = false

Variable template to determine if a type is a floating point type. More...

constexpr autois_floating_point_v< double > = true

Variable template specialisation indicating that double is a floating point type. More...

constexpr autois_floating_point_v< float > = true

Variable template specialisation indicating that float is a floating point type. More...

constexpr autois_floating_point_v< long double > = true

Variable template specialisation indicating that long double is a floating point type. More...

template <class T>
constexpr autois_op_v = __is_base_of (type_traits::op, T)

Variable template to determine if a type derives from op. More...

Description

C++ header file with declarations for the µTest++ type trait utilities and metaprogramming support.

This header provides the declarations for the type trait utilities and metaprogramming constructs used within the µTest++ framework. It defines templates and variable traits for function traits, type lists, identity, value wrappers, compile-time checks for container and floating-point types, and type convertibility.

These utilities underpin advanced template programming, type deduction, and compile-time introspection, supporting the flexible and type-safe design of the framework. The provided traits and wrappers enable expressive and generic handling of types, values, and callable objects, facilitating robust and maintainable test code.

All definitions reside within the micro_os_plus::micro_test_plus::type_traits namespace, ensuring clear separation from user code and minimising the risk of naming conflicts.

All header files are organised within the include/micro-os-plus/micro-test-plus folder to maintain a structured and modular codebase.

This file is intended for internal use within the framework and should not be included directly by user code.

Typedefs

requires_t

template <bool Cond>
using micro_os_plus::micro_test_plus::type_traits::requires_t = typename requires_<Cond>::type

Alias template for extracting the type member from requires_.

Template Parameters
Cond

The boolean condition to be checked at compile time.

The requires_t alias template simplifies the use of the requires_ struct template by directly exposing the nested type member. It is commonly used to enforce compile-time requirements in template declarations.

Definition at line 660 of file type-traits.h.

Functions

declval()

template <class T>
T && micro_os_plus::micro_test_plus::type_traits::declval (void)

Utility function template to simulate std::declval for type deduction.

Template Parameters
T

The type for which an rvalue reference is required.

Parameters

None.

Returns

An rvalue reference to type T.

The declval function template provides a mechanism for obtaining an rvalue reference to a type T without requiring an actual object. This is primarily used in unevaluated contexts, such as within decltype, to deduce types during template metaprogramming in the µTest++ framework.

Definition at line 335 of file type-traits.h.

Referenced by micro_os_plus::micro_test_plus::type_traits::is_convertible and micro_os_plus::micro_test_plus::type_traits::is_valid.

is_convertible()

template <class...>
auto micro_os_plus::micro_test_plus::type_traits::is_convertible (...)
constexpr

Fallback function template for is_convertible, returns false if the conversion is not valid.

Template Parameters
...Unused

Unused template parameters.

Return Values
false

indicating the conversion is not valid.

This overload is selected when the primary is_convertible template cannot be instantiated, providing a false result for invalid conversions.

Definition at line 582 of file type-traits.h.

583 {
584 return false;
585 }

is_convertible()

template <class From, class To>
decltype(bool(To(declval< From >()))) micro_os_plus::micro_test_plus::type_traits::is_convertible (int n)
constexpr

Function template to determine if one type is convertible to another.

Template Parameters
From

The source type to be checked for convertibility.

To

The target type to which conversion is tested.

Parameters
n

Dummy parameter used for overload resolution.

Return Values
true

if From is convertible to To.

The is_convertible function template checks, at compile time, whether a value of type From can be implicitly converted to type To. This is achieved using SFINAE and is primarily used as an implementation detail for the is_convertible_v variable template within the µTest++ framework.

If the conversion is valid, this overload is selected and returns true.

Definition at line 561 of file type-traits.h.

561 is_convertible (int n) -> decltype (bool (To (declval<From> ())))
562 {
563 (void)n; // Prevent the unused parameter warning.
564 return true;
565 }

Reference micro_os_plus::micro_test_plus::type_traits::declval.

is_valid()

template <class... Ts>
bool micro_os_plus::micro_test_plus::type_traits::is_valid (...)
constexpr

Fallback function template for is_valid, returns false if the expression is not valid.

Template Parameters
Ts

The argument types to be tested.

Returns

false indicating the expression is not valid for the given argument types.

This overload is selected when the primary is_valid template cannot be instantiated, providing a false result for invalid expressions.

Definition at line 358 of file type-traits.h.

358 is_valid (...) -> bool
359 {
360 return false;
361 }

is_valid()

template <class... Ts, class Expr_T>
decltype(expr(declval< Ts... >()), bool()) micro_os_plus::micro_test_plus::type_traits::is_valid (Expr_T expr)
constexpr

Definition at line 338 of file type-traits.h.

338 is_valid (Expr_T expr) -> decltype (expr (declval<Ts...> ()), bool ())
339 {
340 return true;
341 }

Reference micro_os_plus::micro_test_plus::type_traits::declval.

Variables

has_epsilon_v

template <class T>
auto micro_os_plus::micro_test_plus::type_traits::has_epsilon_v = is_valid<T> ([] (auto t) -> decltype (void (t.epsilon)) {})
constexpr static

Variable template to determine if a type provides an epsilon member.

Template Parameters
T

The type to be checked for the presence of an epsilon member.

Return Values
true

if T has a member named epsilon.

false

otherwise.

The has_epsilon_v variable template evaluates to true if the given type T defines a member named epsilon. This trait is determined at compile time using SFINAE and the is_valid utility, and is used throughout the µTest++ framework to enable generic handling of types that represent floating-point values or require precision control.

Definition at line 442 of file type-traits.h.

443 = is_valid<T> ([] (auto t) -> decltype (void (t.epsilon)) {});

has_npos_v

template <class T>
auto micro_os_plus::micro_test_plus::type_traits::has_npos_v = is_valid<T> ([] (auto t) -> decltype (void (t.npos)) {})
constexpr static

Variable template to determine if a type provides a static npos member.

Template Parameters
T

The type to be checked for the presence of a static npos member.

Return Values
true

if T has a static member named npos.

false

otherwise.

The has_npos_v variable template evaluates to true if the given type T defines a static member named npos. This trait is determined at compile time using SFINAE and the is_valid utility, and is used throughout the µTest++ framework to enable generic handling of types that follow the standard string or container conventions.

Definition at line 401 of file type-traits.h.

402 = is_valid<T> ([] (auto t) -> decltype (void (t.npos)) {});

Referenced by micro_os_plus::micro_test_plus::test_reporter::operator<<.

has_value_v

template <class T>
auto micro_os_plus::micro_test_plus::type_traits::has_value_v = is_valid<T> ([] (auto t) -> decltype (void (t.value)) {})
constexpr static

Variable template to determine if a type provides a value member.

Template Parameters
T

The type to be checked for the presence of a value member.

Return Values
true

if T has a member named value.

false

otherwise.

The has_value_v variable template evaluates to true if the given type T defines a member named value. This trait is determined at compile time using SFINAE and the is_valid utility, and is used throughout the µTest++ framework to enable generic handling of types that encapsulate a value, such as wrappers or constant types.

Definition at line 421 of file type-traits.h.

422 = is_valid<T> ([] (auto t) -> decltype (void (t.value)) {});

is_container_v

template <class T>
auto micro_os_plus::micro_test_plus::type_traits::is_container_v
constexpr static

Variable template to determine if a type models a container.

Initialiser
= is_valid<T> ( [] (auto t) -> decltype (t.begin (), t.end (), void ()) {})
Template Parameters
T

The type to be checked for container-like behaviour.

Return Values
true

if T has both begin() and end() member functions.

false

otherwise.

The is_container_v variable template evaluates to true if the given type T provides both begin() and end() member functions, indicating that it models a standard container concept. This trait is determined at compile time using SFINAE and is_valid, and is used throughout the µTest++ framework to enable generic handling of container types in template metaprogramming.

Definition at line 380 of file type-traits.h.

380 static constexpr auto is_container_v = is_valid<T> (

is_convertible_v

template <class From, class To>
auto micro_os_plus::micro_test_plus::type_traits::is_convertible_v = is_convertible<From, To> (0)
constexpr

Variable template to determine if one type is convertible to another.

Template Parameters
From

The source type to be checked for convertibility.

To

The target type to which conversion is tested.

Return Values
true

if From is convertible to To.

false

otherwise.

The is_convertible_v variable template evaluates to true if the type From is implicitly convertible to the type To, and false otherwise. This trait is determined at compile time and is used throughout the µTest++ framework to enable type-safe conversions and requirements checking in template metaprogramming.

Definition at line 605 of file type-traits.h.

is_floating_point_v

template <class T>
auto micro_os_plus::micro_test_plus::type_traits::is_floating_point_v = false
constexpr

Variable template to determine if a type is a floating point type.

Template Parameters
T

The type to be checked for floating point classification.

Return Values
true

if T is a floating point type.

false

otherwise.

The is_floating_point_v variable template evaluates to true if the given type T is a floating point type (float, double, or long double). For all other types, it evaluates to false. This trait is used throughout the µTest++ framework to enable type-safe handling and specialisation for floating point types in template metaprogramming.

Specialisations are provided for float, double, and long double, each evaluating to true.

Definition at line 465 of file type-traits.h.

465 inline constexpr auto is_floating_point_v = false;

is_floating_point_v< double >

auto micro_os_plus::micro_test_plus::type_traits::is_floating_point_v< double > = true
constexpr

Variable template specialisation indicating that double is a floating point type.

This specialisation of the is_floating_point_v variable template evaluates to true for the double type, confirming that it is recognised as a floating point type within the µTest++ framework. This enables type-safe handling and specialisation for floating point types in template metaprogramming.

See Also

is_floating_point_v

Definition at line 497 of file type-traits.h.

497 inline constexpr auto is_floating_point_v<double> = true;

is_floating_point_v< float >

auto micro_os_plus::micro_test_plus::type_traits::is_floating_point_v< float > = true
constexpr

Variable template specialisation indicating that float is a floating point type.

This specialisation of the is_floating_point_v variable template evaluates to true for the float type, confirming that it is recognised as a floating point type within the µTest++ framework. This enables type-safe handling and specialisation for floating point types in template metaprogramming.

See Also

is_floating_point_v

Definition at line 481 of file type-traits.h.

481 inline constexpr auto is_floating_point_v<float> = true;

is_floating_point_v< long double >

auto micro_os_plus::micro_test_plus::type_traits::is_floating_point_v< long double > = true
constexpr

Variable template specialisation indicating that long double is a floating point type.

This specialisation of the is_floating_point_v variable template evaluates to true for the long double type, confirming that it is recognised as a floating point type within the µTest++ framework. This enables type-safe handling and specialisation for floating point types in template metaprogramming.

See Also

is_floating_point_v

Definition at line 513 of file type-traits.h.

513 inline constexpr auto is_floating_point_v<long double> = true;

is_op_v

template <class T>
auto micro_os_plus::micro_test_plus::type_traits::is_op_v = __is_base_of (type_traits::op, T)
constexpr

Variable template to determine if a type derives from op.

Template Parameters
T

The type to be checked for derivation from op.

Return Values
true

if T is derived from type_traits::op.

false

otherwise.

The is_op_v variable template evaluates to true if the given type T is derived from the type_traits::op base struct, and false otherwise. This trait is determined at compile time using compiler intrinsics and is used throughout the µTest++ framework to enable generic handling and detection of operator-like or value wrapper types in template metaprogramming.

Definition at line 944 of file type-traits.h.

944 inline constexpr auto is_op_v = __is_base_of (type_traits::op, T);

Referenced by micro_os_plus::micro_test_plus::test_reporter::fail.

File Listing

The file content with the documentation metadata removed is:

1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
47
48#ifndef MICRO_TEST_PLUS_TYPE_TRAITS_H_
49#define MICRO_TEST_PLUS_TYPE_TRAITS_H_
50
51// ----------------------------------------------------------------------------
52
53#ifdef __cplusplus
54
55// ----------------------------------------------------------------------------
56
57#include "math.h"
58
59// ----------------------------------------------------------------------------
60
61#if defined(__GNUC__)
62#pragma GCC diagnostic push
63#if defined(__clang__)
64#pragma clang diagnostic ignored "-Wc++98-compat"
65#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
66#endif
67#endif
68
70{
71 // --------------------------------------------------------------------------
72
97 namespace type_traits
98 {
117 template <class... Types>
118 struct list
119 {
120 };
121
140 template <class T, class... Extra>
141 struct identity
142 {
146 using type = T;
147 };
148
149#if defined(__DOXYGEN__)
150 // error: Detected potential recursive class relation between class
151 // micro_os_plus::micro_test_plus::type_traits::function_traits and base
152 // class micro_os_plus::micro_test_plus::type_traits::function_traits<
153 // decltype(&T::operator())>!
154 // https://github.com/doxygen/doxygen/issues/9915
155#else
176 template <class T>
177 struct function_traits : function_traits<decltype (&T::operator())>
178 {
179 };
180#endif
181
201 template <class R, class... Args_T>
202 struct function_traits<R (*) (Args_T...)>
203 {
207 using result_type = R;
208
212 using args = list<Args_T...>;
213 };
214
234 template <class R, class... Args_T>
235 struct function_traits<R (Args_T...)>
236 {
240 using result_type = R;
241
245 using args = list<Args_T...>;
246 };
247
268 template <class R, class T, class... Args_T>
269 struct function_traits<R (T::*) (Args_T...)>
270 {
274 using result_type = R;
275
279 using args = list<Args_T...>;
280 };
281
303 template <class R, class T, class... Args_T>
304 struct function_traits<R (T::*) (Args_T...) const>
305 {
309 using result_type = R;
310
314 using args = list<Args_T...>;
315 };
316
333 template <class T>
334 T&&
335 declval (void);
336 template <class... Ts, class Expr_T>
337 constexpr auto
338 is_valid (Expr_T expr) -> decltype (expr (declval<Ts...> ()), bool ())
339 {
340 return true;
341 }
342
356 template <class... Ts>
357 constexpr auto
358 is_valid (...) -> bool
359 {
360 return false;
361 }
362
379 template <class T>
380 static constexpr auto is_container_v = is_valid<T> (
381 [] (auto t) -> decltype (t.begin (), t.end (), void ()) {});
382
400 template <class T>
401 static constexpr auto has_npos_v
402 = is_valid<T> ([] (auto t) -> decltype (void (t.npos)) {});
403
420 template <class T>
421 static constexpr auto has_value_v
422 = is_valid<T> ([] (auto t) -> decltype (void (t.value)) {});
423
441 template <class T>
442 static constexpr auto has_epsilon_v
443 = is_valid<T> ([] (auto t) -> decltype (void (t.epsilon)) {});
444
464 template <class T>
465 inline constexpr auto is_floating_point_v = false;
466
480 template <>
481 inline constexpr auto is_floating_point_v<float> = true;
482
496 template <>
497 inline constexpr auto is_floating_point_v<double> = true;
498
512 template <>
513 inline constexpr auto is_floating_point_v<long double> = true;
514
515#if defined(__clang__) or defined(_MSC_VER)
536 template <class From, class To>
537 static constexpr auto is_convertible_v = __is_convertible_to (From, To);
538#else
559 template <class From, class To>
560 constexpr auto
561 is_convertible (int n) -> decltype (bool (To (declval<From> ())))
562 {
563 (void)n; // Prevent the unused parameter warning.
564 return true;
565 }
566
580 template <class...>
581 constexpr auto
583 {
584 return false;
585 }
586
604 template <class From, class To>
606#endif
607
624 template <bool Cond>
625 struct requires_
626 {
627 };
628
639 template <>
641 {
645 using type = int;
646 };
647
659 template <bool Cond>
661
678 struct op
679 {
680 };
681
700 template <auto N>
702 {
706 using value_type = decltype (N);
707
711 static constexpr auto value = N;
712
722 [[nodiscard]] constexpr auto
723 operator- () const
724 {
725 return integral_constant<-N>{};
726 }
727
738 [[nodiscard]] constexpr explicit
739 operator value_type (void) const
740 {
741 return N;
742 }
743
754 [[nodiscard]] constexpr auto
755 get (void) const
756 {
757 return N;
758 }
759 };
760
787 template <class T, auto N, auto D, auto Size, auto P = 1>
789 {
793 using value_type = T;
794
801 static constexpr auto epsilon = T (1) / math::pow (T (10), Size - 1);
802
809 static constexpr auto value
810 = T (P) * (T (N) + (T (D) / math::pow (T (10), Size)));
811
821 [[nodiscard]] constexpr auto
822 operator-() const
823 {
824 return floating_point_constant<T, N, D, Size, -1>{};
825 }
826
836 [[nodiscard]] constexpr explicit
837 operator value_type () const
838 {
839 return value;
840 }
841
850 [[nodiscard]] constexpr auto
851 get () const
852 {
853 return value;
854 }
855 };
856
876 template <class T>
878 {
882 using value_type = T;
883
889 constexpr genuine_integral_value (const T& _value) : value_{ _value }
890 {
891 }
892
901 [[nodiscard]] constexpr explicit
902 operator T () const
903 {
904 return value_;
905 }
906
915 [[nodiscard]] constexpr decltype (auto)
916 get () const
917 {
918 return value_;
919 }
920
924 T value_{};
925 };
926
943 template <class T>
944 inline constexpr auto is_op_v = __is_base_of (type_traits::op, T);
945
968 template <class T, class Opt = int>
970 {
974 using value_type = T;
975
981 constexpr value (const T& _value) : value_{ _value }
982 {
983 }
984
993 [[nodiscard]] constexpr explicit
994 operator T () const
995 {
996 return value_;
997 }
998
1009 [[nodiscard]] constexpr decltype (auto)
1010 get (void) const
1011 {
1012 return value_;
1013 }
1014
1019 };
1020
1046 template <class T>
1047 struct value<T,
1048 type_traits::requires_t<type_traits::is_floating_point_v<T>>>
1050 {
1054 using value_type = T;
1055
1064 static inline auto epsilon = T{}; // Why static?
1065
1072 constexpr value (const T& _value, const T precision) : value_{ _value }
1073 {
1074 epsilon = precision;
1075 }
1076
1086 constexpr /*explicit(false)*/ value (const T& val)
1087 : value{ val,
1088 T (1)
1089 / math::pow (T (10),
1090 math::den_size<unsigned long long> (val)) }
1091 {
1092 }
1093
1102 [[nodiscard]] constexpr explicit
1103 operator T () const
1104 {
1105 return value_;
1106 }
1107
1118 [[nodiscard]] constexpr decltype (auto)
1119 get (void) const
1120 {
1121 return value_;
1122 }
1123
1128 };
1129
1130 // ------------------------------------------------------------------------
1131 } // namespace type_traits
1132
1133 // --------------------------------------------------------------------------
1134} // namespace micro_os_plus::micro_test_plus
1135
1136#if defined(__GNUC__)
1137#pragma GCC diagnostic pop
1138#endif
1139
1140// ----------------------------------------------------------------------------
1141
1142#endif // __cplusplus
1143
1144// ----------------------------------------------------------------------------
1145
1146#endif // MICRO_TEST_PLUS_TYPE_TRAITS_H_
1147
1148// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.14.0.