Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
default_constructible_properties.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_TESTING_DEFAULT_CONSTRUCTIBLE_PROPERTIES_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_TESTING_DEFAULT_CONSTRUCTIBLE_PROPERTIES_HPP_
7
8#include "arene/base/compiler_support/platform_queries.hpp"
9#include "arene/base/compiler_support/preprocessor.hpp"
10#include "arene/base/constraints/constraints.hpp"
11#include "arene/base/stdlib_choice/enable_if.hpp"
12#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
13#include "arene/base/stdlib_choice/is_default_constructible.hpp"
14#include "arene/base/stdlib_choice/terminate.hpp"
15#include "arene/base/type_info/type_name_string.hpp" // IWYU pragma: keep
16#include "testlibs/utilities/constexpr_traits.hpp"
17
18#if ARENE_IS_OFF(ARENE_STDLIB_LIBARENECXX)
19#include <iostream>
20#endif
21
22namespace arene {
23namespace base {
24namespace testing {
25namespace detail {
26
27/// @brief Assert that two default constructed instances are equal
28/// @tparam T The type to check
29///
30/// The default implementation for types that are not default constructible does not assert.
31template <class T, arene::base::constraints<std::enable_if_t<!std::is_default_constructible<T>::value>> = nullptr>
32constexpr auto assert_default_constructed_instances_are_equal() -> void {}
33
34/// @brief Assert that two default constructed instances are equal
35/// @tparam T The type to check
36///
37/// For types that are not default constructible in a constant expression, the equality is checked once at runtime then
38/// cached.
39template <
40 class T,
41 arene::base::constraints<
42 std::enable_if_t<std::is_default_constructible<T>::value>,
43 std::enable_if_t<!::testing::is_constexpr_default_constructible_v<T>>> = nullptr>
44auto assert_default_constructed_instances_are_equal() -> void {
45 if (!(T{} == T{})) {
46#if ARENE_IS_OFF(ARENE_STDLIB_LIBARENECXX)
47 std::cerr << "ERROR: test provided a default constructible value(" << arene::base::type_name_v<T>.c_str() << ")"
48 << " but two default constructed instances do not compare equal\n";
49#endif
50 std::terminate();
51 }
52}
53
54/// @brief Assert that two default constructed instances are equal
55/// @tparam T The type to check
56///
57/// For types that are default constructible in a constant expression, the equality is checked in a static assertion.
58template <
59 class T,
60 arene::base::constraints<
61 std::enable_if_t<std::is_default_constructible<T>::value>,
62 std::enable_if_t<::testing::is_constexpr_default_constructible_v<T>>> = nullptr>
63constexpr auto assert_default_constructed_instances_are_equal() -> void {
64 // Note: If the condition is directly placed in the static_assert, gcc8 will always execute it, even when this
65 // overload is not chosen. Putting the conditional result in a variable avoids this issue.
66 constexpr bool are_equal = T{} == T{};
67 static_assert(
68 are_equal,
69 "test provided a default constructible value, but two default constructed instances do not compare equal"
70 );
71}
72
73/// @brief Assert that a copy of a default constructed instance equals the original
74/// @tparam T The type to check
75///
76/// The default implementation for types that are not default constructible or copy constructible does not assert.
77template <
78 class T,
79 arene::base::constraints<
80 std::enable_if_t<!std::is_default_constructible<T>::value || !std::is_copy_constructible<T>::value>> = nullptr>
81constexpr auto assert_copied_value_equals_original() -> void {}
82
83/// @brief Assert that a copy of a default constructed instance equals the original
84/// @tparam T The type to check
85///
86/// For types that are not default constructible or copy constructible in a constant expression, the equality is checked
87/// once at runtime and cached.
88template <
89 class T,
90 arene::base::constraints<
91 std::enable_if_t<std::is_default_constructible<T>::value>,
92 std::enable_if_t<std::is_copy_constructible<T>::value>,
93 std::enable_if_t<!::testing::is_constexpr_default_and_copy_constructible_v<T>>> = nullptr>
94auto assert_copied_value_equals_original() -> void {
95 auto const original = T{};
96 auto const copy = original;
97 if (!(original == copy)) {
98#if ARENE_IS_OFF(ARENE_STDLIB_LIBARENECXX)
99 std::cerr << "ERROR: test provided a default constructible value(" << arene::base::type_name_v<T>.c_str() << ")"
100 << " but a copy of the default constructed instance does not compare equal to the original\n";
101#endif
102 std::terminate();
103 }
104}
105
106/// @brief Assert that a copy of a default constructed instance equals the original
107/// @tparam T The type to check
108///
109/// For types that are default constructible and copy constructible in a constant expression, the equality is checked in
110/// a static assertion.
111template <
112 class T,
113 arene::base::constraints<
114 std::enable_if_t<std::is_default_constructible<T>::value>,
115 std::enable_if_t<std::is_copy_constructible<T>::value>,
116 std::enable_if_t<::testing::is_constexpr_default_and_copy_constructible_v<T>>> = nullptr>
117constexpr auto assert_copied_value_equals_original() -> void {
118 constexpr auto original = T{};
119 constexpr auto copy = original;
120
121 // Note: If the condition is directly placed in the static_assert, gcc8 will always execute it, even when this
122 // overload is not chosen. Putting the conditional result in a variable avoids this issue.
123 constexpr auto are_equal = original == copy;
124 static_assert(
125 are_equal,
126 "test provided a default constructible value, but a copy of the default constructed instance does not compare "
127 "equal to the original"
128 );
129}
130} // namespace detail
131
132/// @brief Assert default constructible types have the expected properties
133/// @tparam T The type to check
134///
135/// For types that are default constructible, two default constructed instances must compare equal.
136/// For types that are also copy constructible, a copy of a default constructed instance must compare equal to the
137/// original.
138template <class T>
140 detail::assert_default_constructed_instances_are_equal<T>();
141 detail::assert_copied_value_equals_original<T>();
142}
143
144} // namespace testing
145} // namespace base
146} // namespace arene
147
148#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_TESTING_DEFAULT_CONSTRUCTIBLE_PROPERTIES_HPP_
Definition customization.hpp:36
constexpr auto assert_default_constructible_properties() -> void
Assert default constructible types have the expected properties.
Definition default_constructible_properties.hpp:139
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10