Skip to main content

The type-traits.h File Reference

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_os_plus::micro_test_plus

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

namespacemicro_os_plus::micro_test_plus::type_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, class...>

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

structintegral_constant<N>

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

structlist<class...>

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

structop

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

structrequires_<bool>

Struct template for SFINAE requirements. More...

structrequires_<true>

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

structvalue<T, class>

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

usingrequires_t = typename requires_< Cond >::type

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

Variables Index

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

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

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

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

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

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

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

autois_convertible_v = is_convertible<From, To> (0)

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

autois_floating_point_v = false

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

autois_floating_point_v< double > = true

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

autois_floating_point_v< float > = true

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

autois_floating_point_v< long double > = true

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

autois_op_v = __is_base_of (type_traits::op, T)

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

Functions Index

T &&declval (void)

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

constexpr autois_convertible (...)

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

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

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

constexpr boolis_valid (...)

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

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

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
CondThe 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 662 of file type-traits.h.

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)) {})
constexprstatic

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

Template Parameters
TThe type to be checked for the presence of an epsilon member.
Return Values
trueif T has a member named epsilon.
falseotherwise.

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.

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)) {})
constexprstatic

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

Template Parameters
TThe type to be checked for the presence of a static npos member.
Return Values
trueif T has a static member named npos.
falseotherwise.

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.

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)) {})
constexprstatic

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

Template Parameters
TThe type to be checked for the presence of a value member.
Return Values
trueif T has a member named value.
falseotherwise.

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.

is_container_v

template <class T>
auto micro_os_plus::micro_test_plus::type_traits::is_container_v = is_valid<T> ( [] (auto t) -> decltype (t.begin (), t.end (), void ()) {})
constexprstatic

Variable template to determine if a type models a container.

Template Parameters
TThe type to be checked for container-like behaviour.
Return Values
trueif T has both begin() and end() member functions.
falseotherwise.

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.

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
FromThe source type to be checked for convertibility.
ToThe target type to which conversion is tested.
Return Values
trueif From is convertible to To.
falseotherwise.

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
TThe type to be checked for floating point classification.
Return Values
trueif T is a floating point type.
falseotherwise.

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.

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.

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.

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.

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
TThe type to be checked for derivation from op.
Return Values
trueif T is derived from type_traits::op.
falseotherwise.

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 946 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
TThe 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.

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
...UnusedUnused template parameters.
Return Values
falseindicating 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.

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
FromThe source type to be checked for convertibility.
ToThe target type to which conversion is tested.
Parameters
nDummy parameter used for overload resolution.
Return Values
trueif 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.

is_valid()

template <class...>
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
...TsThe 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.

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.

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 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any 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
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&#95;1&#95;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...>
118 struct list
119 {
120 };
121
140 template <class T, class...>
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())>!
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...>
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
626 template <bool>
627 struct requires_
628 {
629 };
630
641 template <>
643 {
647 using type = int;
648 };
649
661 template <bool Cond>
663
680 struct op
681 {
682 };
683
702 template <auto N>
704 {
708 using value_type = decltype (N);
709
713 static constexpr auto value = N;
714
724 [[nodiscard]] constexpr auto
725 operator- () const
726 {
727 return integral_constant<-N>{};
728 }
729
740 [[nodiscard]] constexpr explicit
741 operator value_type (void) const
742 {
743 return N;
744 }
745
756 [[nodiscard]] constexpr auto
757 get (void) const
758 {
759 return N;
760 }
761 };
762
789 template <class T, auto N, auto D, auto Size, auto P = 1>
791 {
795 using value_type = T;
796
803 static constexpr auto epsilon = T (1) / math::pow (T (10), Size - 1);
804
811 static constexpr auto value
812 = T (P) * (T (N) + (T (D) / math::pow (T (10), Size)));
813
823 [[nodiscard]] constexpr auto
824 operator-() const
825 {
826 return floating_point_constant<T, N, D, Size, -1>{};
827 }
828
838 [[nodiscard]] constexpr explicit
839 operator value_type () const
840 {
841 return value;
842 }
843
852 [[nodiscard]] constexpr auto
853 get () const
854 {
855 return value;
856 }
857 };
858
878 template <class T>
880 {
884 using value_type = T;
885
891 constexpr genuine_integral_value (const T& _value) : value_{ _value }
892 {
893 }
894
903 [[nodiscard]] constexpr explicit
904 operator T () const
905 {
906 return value_;
907 }
908
917 [[nodiscard]] constexpr decltype (auto)
918 get () const
919 {
920 return value_;
921 }
922
926 T value_{};
927 };
928
945 template <class T>
946 inline constexpr auto is_op_v = __is_base_of (type_traits::op, T);
947
970 template <class T, class = int>
972 {
976 using value_type = T;
977
983 constexpr value (const T& _value) : value_{ _value }
984 {
985 }
986
995 [[nodiscard]] constexpr explicit
996 operator T () const
997 {
998 return value_;
999 }
1000
1011 [[nodiscard]] constexpr decltype (auto)
1012 get (void) const
1013 {
1014 return value_;
1015 }
1016
1021 };
1022
1048 template <class T>
1049 struct value<T,
1050 type_traits::requires_t<type_traits::is_floating_point_v<T>>>
1052 {
1056 using value_type = T;
1057
1066 static inline auto epsilon = T{}; // Why static?
1067
1074 constexpr value (const T& _value, const T precision) : value_{ _value }
1075 {
1076 epsilon = precision;
1077 }
1078
1088 constexpr /*explicit(false)*/ value (const T& val)
1089 : value{ val,
1090 T (1)
1091 / math::pow (T (10),
1092 math::den_size<unsigned long long> (val)) }
1093 {
1094 }
1095
1104 [[nodiscard]] constexpr explicit
1105 operator T () const
1106 {
1107 return value_;
1108 }
1109
1120 [[nodiscard]] constexpr decltype (auto)
1121 get (void) const
1122 {
1123 return value_;
1124 }
1125
1130 };
1131
1132 // ------------------------------------------------------------------------
1133 } // namespace type_traits
1134
1135 // --------------------------------------------------------------------------
1136} // namespace micro_os_plus::micro_test_plus
1137
1138#if defined(__GNUC__)
1139#pragma GCC diagnostic pop
1140#endif
1141
1142// ----------------------------------------------------------------------------
1143
1144#endif // __cplusplus
1145
1146// ----------------------------------------------------------------------------
1147
1148#endif // MICRO_TEST_PLUS_TYPE_TRAITS_H_
1149
1150// ----------------------------------------------------------------------------

Generated via docusaurus-plugin-doxygen by Doxygen 1.14.0.