Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
abs_cpo.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: abs_impl implements the abs function as a CPO"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ABS_CPO_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ABS_CPO_HPP_
9
10// IWYU pragma: private, include "arene/base/math.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
17#include "arene/base/stdlib_choice/declval.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
20
21// parasoft-begin-suppress AUTOSAR-M17_0_3-a "False positive: this name is not reserved in namespace arene::base::math"
22// parasoft-begin-suppress CERT_CPP-DCL51-f "False positive: this name is not reserved in namespace arene::base::math"
23
24namespace arene {
25namespace base {
26namespace math {
27
28namespace abs_detail {
29
30// parasoft-begin-suppress CERT_C-EXP37-a "False positive: rule does not mention naming all parameters"
31// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: function does not have internal linkage"
32
33/// @brief Dummy declaration of the ADL customization point so the call below will be well-formed but not match
34/// @tparam U A type being checked for abs overloads
35template <typename U>
36auto abs(U) -> U = delete;
37
38// parasoft-end-suppress CERT_C-EXP37-a-3
39// parasoft-end-suppress AUTOSAR-M3_3_2-a
40
41/// @brief Type trait to determine if a type has a const member function called @c abs()
42/// @tparam T The type to check
43template <typename T, typename = constraints<>>
44constexpr bool has_member_abs_v{false};
45
46/// @brief Type trait to determine if a type has a const member function called @c abs()
47/// @tparam T The type to check
48template <typename T>
49constexpr bool has_member_abs_v<T, constraints<decltype(std::declval<T const&>().abs())>>{true};
50
51/// @brief Type trait to determine if a type has an ADL-visible free function <c>abs(T const&)</c>
52/// @tparam T The type to check
53template <typename T, typename = constraints<>>
54constexpr bool has_adl_abs_v{false};
55
56/// @brief Type trait to determine if a type has an ADL-visible free function <c>abs(T const&)</c>
57/// @tparam T The type to check
58template <typename T>
59constexpr bool has_adl_abs_v<T, constraints<decltype(abs(std::declval<T const&>()))>>{true};
60
61/// @brief Function object class implementing @c arene::base::math::abs
62class abs_impl {
63 public:
64 /// @brief Obtain the absolute value of a value
65 /// @tparam T Type of the value to obtain the absolute value of
66 /// @param value Value to obtain the absolute value of
67 /// @return The absolute value of @c value
68 template <typename T, constraints<std::enable_if_t<has_member_abs_v<T>>> = nullptr>
69 constexpr auto operator()(T const& value) const noexcept(noexcept(std::declval<T const&>().abs()))
70 -> decltype(value.abs()) {
71 return value.abs();
72 }
73
74 /// @brief Obtain the absolute value of a value
75 /// @tparam T Type of the value to obtain the absolute value of
76 /// @param value Value to obtain the absolute value of
77 /// @return The absolute value of @c value
78 template <typename T, constraints<std::enable_if_t<!has_member_abs_v<T> && has_adl_abs_v<T>>> = nullptr>
79 constexpr auto operator()(T const& value) const noexcept(noexcept(abs(std::declval<T const&>())))
80 -> decltype(abs(value)) {
81 return abs(value);
82 }
83
84 /// @brief Obtain the absolute value of a value
85 /// @tparam T Type of the value to obtain the absolute value of
86 /// @param value Value to obtain the absolute value of
87 /// @return The absolute value of @c value
88 template <
89 typename T,
91 constexpr auto operator()(T const& value) const noexcept -> decltype(::arene::base::abs(value)) {
92 return ::arene::base::abs(value);
93 }
94};
95} // namespace abs_detail
96
97// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object."
98// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object."
99/// @brief Obtain the absolute value using a member function, an ADL overload, or @c arene::base::abs
100ARENE_CPP14_INLINE_VARIABLE(abs_detail::abs_impl, abs);
101// parasoft-end-suppress AUTOSAR-M7_3_3-a
102// parasoft-end-suppress CERT_CPP-DCL59-a
103
104} // namespace math
105} // namespace base
106} // namespace arene
107
108// parasoft-end-suppress AUTOSAR-M17_0_3-a
109// parasoft-end-suppress CERT_CPP-DCL51-f
110
111#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ABS_CPO_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition abs_cpo.hpp:26
Definition accumulate.hpp:37
constexpr auto abs(Number value) noexcept -> Number
Calculate the absolute value of a number.
Definition abs.hpp:46
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...