Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
divides.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_STDLIB_INCLUDE_STDLIB_DETAIL_DIVIDES_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_DIVIDES_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10
11// IWYU pragma: private, include <functional>
12// IWYU pragma: friend "stdlib_detail/.*"
13
14#include "arene/base/constraints.hpp"
15#include "stdlib/include/stdlib_detail/declval.hpp"
16#include "stdlib/include/stdlib_detail/enable_if.hpp"
17#include "stdlib/include/stdlib_detail/forward.hpp"
18#include "stdlib/include/stdlib_detail/is_integral.hpp"
19
20namespace std {
21
22/// @brief Function object for performing division.
23/// @tparam T The type of the values
24/// @note This class contains the member types @c result_type, @c first_argument type, and @c second_argument type.
25/// These aliases have been deprecated in C++17 and removed in C++20.
26template <class T = void>
27class divides {
28 public:
29 /// @brief Result type of invoking this object
30 using result_type = T;
31 /// @brief Type of the first value to divide
32 using first_argument_type = T;
33 /// @brief Type of the second value to divide
34 using second_argument_type = T;
35
36 /// @brief Returns the first value divided by second value
37 /// @tparam U Template parameter to enable constraints.
38 /// @param lhs The first value to divide
39 /// @param rhs The second value to divide
40 /// @return Returns the result of invoking @c operator/ on @c lhs and @c rhs
41 template <class U = T, arene::base::constraints<std::enable_if_t<!is_integral_v<U>>> = nullptr>
42 constexpr auto operator()(T const& lhs, T const& rhs) const
43 noexcept(noexcept(std::declval<T&>() / std::declval<T&>())) -> T {
44 return lhs / rhs;
45 }
46
47 /// @brief Returns the first value divided by second value
48 /// @tparam U Template parameter to enable constraints.
49 /// @param lhs The first value to divide
50 /// @param rhs The second value to divide
51 /// @return Returns the result of invoking @c operator/ on @c lhs and @c rhs
52 ///
53 /// @note This overload explicitly casts the result of the computation to the type @c T. This avoids any warning for
54 /// implicit conversion (present on gcc8).
55 template <class U = T, arene::base::constraints<std::enable_if_t<is_integral_v<U>>> = nullptr>
56 constexpr auto operator()(T const& lhs, T const& rhs) const
57 noexcept(noexcept(std::declval<T&>() / std::declval<T&>())) -> T {
58 return static_cast<T>(lhs / rhs);
59 }
60};
61
62/// @brief Function object that can be invoked for division, specialized for type deduction
63template <>
64class divides<void> {
65 public:
66 /// @brief Denotes that this is a transparent function object type
67 using is_transparent = void;
68
69 // parasoft-begin-suppress AUTOSAR-M5_0_4-a "Function return type is specified by the C++ Standard"
70 /// @brief Returns the first value divided by second value
71 /// @tparam T The first type
72 /// @tparam U The second type
73 /// @param lhs The first value to divide
74 /// @param rhs The second value to divide
75 /// @return Returns the result of invoking @c operator/ on @c lhs and @c rhs
76 template <class T, class U>
77 constexpr auto operator()(T&& lhs, U&& rhs) const
78 noexcept(noexcept(std::forward<T>(std::declval<T&>()) / std::forward<U>(std::declval<U&>())))
79 -> decltype(std::forward<T>(lhs) / std::forward<U>(rhs)) {
80 return std::forward<T>(lhs) / std::forward<U>(rhs);
81 }
82 // parasoft-end-suppress AUTOSAR-M5_0_4-a
83};
84
85} // namespace std
86
87#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_DIVIDES_HPP_
constexpr auto operator()(T &&lhs, U &&rhs) const noexcept(noexcept(std::forward< T >(std::declval< T & >())/std::forward< U >(std::declval< U & >()))) ->
Returns the first value divided by second value.
Definition divides.hpp:77
Function object for performing division.
Definition divides.hpp:27
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827