Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
construct_at.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::construct_at"
2
3// Copyright 2024, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MEMORY_CONSTRUCT_AT_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MEMORY_CONSTRUCT_AT_HPP_
8
9// IWYU pragma: private, include "arene/base/memory.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
12#include "arene/base/compiler_support/cpp14_inline.hpp"
13#include "arene/base/constraints/constraints.hpp"
14#include "arene/base/constraints/substitution_succeeds.hpp"
15#include "arene/base/stdlib_choice/declval.hpp"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17#include "arene/base/stdlib_choice/forward.hpp"
18#include "arene/base/stdlib_choice/is_constructible.hpp"
19#include "arene/base/type_manipulation/give_qualifiers_to.hpp"
20
21// parasoft-begin-suppress AUTOSAR-A16_2_2-a "This header is required on CR builds"
22#include "arene/base/stdlib_choice/new.hpp" // IWYU pragma: keep
23// parasoft-end-suppress AUTOSAR-A16_2_2-a
24
25// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
26
27namespace arene {
28namespace base {
29
30namespace construct_at_detail {
31/// @brief Trait which deduces the type that would be returned by a placement new call
32template <typename T, typename... Args>
33using try_construct = decltype(::new (std::declval<void*>()) T(std::declval<Args>()...));
34
35/// @brief Trait which determins if a given type can be placement-new instantiated with the given arguments.
36template <typename T, typename... Args>
37constexpr bool is_constructible = substitution_succeeds<try_construct, T, Args...>;
38
39/// @brief Implementation helper for @c arene::base::construct_at
40class do_construct_at {
41 public:
42 // parasoft-begin-suppress CERT_CPP-ERR55-a-2 "False positive: Exception specification is conditional"
43 // parasoft-begin-suppress CERT_CPP-ERR50-g-3 "False positive: Exception specification is conditional"
44 // parasoft-begin-suppress CERT_CPP-ERR50-h-3 "False positive: Exception specification is conditional"
45 // parasoft-begin-suppress CERT_CPP-ERR51-b-3 "False positive: Exception specification is conditional"
46 // parasoft-begin-suppress AUTOSAR-A15_5_3-h-2 "False positive: Exception specification is conditional"
47 // parasoft-begin-suppress AUTOSAR-A15_5_3-g-2 "False positive: Exception specification is conditional"
48 // parasoft-begin-suppress AUTOSAR-A15_3_2-a-2 "False positive: Exception specification is conditional"
49 // parasoft-begin-suppress AUTOSAR-M15_3_4-b-2 "False positive: Exception specification is conditional"
50 // parasoft-begin-suppress AUTOSAR-A15_2_1-b-2 "False positive: Not invoked during initialization"
51 /// @brief Do the construction
52 /// @tparam T The type to construct
53 /// @tparam Args The types of the arguments
54 /// @param ptr The pointer to the storage
55 /// @param args The arguments to pass
56 /// @return A pointer to the newly constructed object
57 /// @see arene::base::construct_at
58 /// @throws Any exception thrown by the constructor of @c T
59 template <class T, class... Args, constraints<std::enable_if_t<is_constructible<T, Args...>>> = nullptr>
60 auto operator()(T* ptr, Args&&... args) const noexcept(std::is_nothrow_constructible<T, Args...>::value) -> T* {
61 // parasoft-begin-suppress AUTOSAR-A5_2_3-a-2 "False positive: This is creating a new object, it only removes
62 // qualifiers from the storage, which are used to determine the qualifiers on the new object"
63 // parasoft-begin-suppress CERT_CPP-EXP55-a-2 "False positive: This is creating a new object, it only removes
64 // qualifiers from the storage, which are used to determine the qualifiers on the new object"
65 // Justification: False positive about newly created resource
66 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory,cppcoreguidelines-pro-type-const-cast)
67 return ::new (const_cast<void*>(static_cast<give_qualifiers_to<T, void>*>(ptr))) T(std::forward<Args>(args)...);
68 // parasoft-end-suppress AUTOSAR-A5_2_3-a-2
69 // parasoft-end-suppress CERT_CPP-EXP55-a-2
70 }
71 // parasoft-end-suppress CERT_CPP-ERR55-a-2
72 // parasoft-end-suppress CERT_CPP-ERR50-g-3
73 // parasoft-end-suppress CERT_CPP-ERR50-h-3
74 // parasoft-end-suppress CERT_CPP-ERR51-b-3
75 // parasoft-end-suppress AUTOSAR-A15_5_3-h-2
76 // parasoft-end-suppress AUTOSAR-A15_5_3-g-2
77 // parasoft-end-suppress AUTOSAR-A15_3_2-a-2
78 // parasoft-end-suppress AUTOSAR-M15_3_4-b-2
79 // parasoft-end-suppress AUTOSAR-A15_2_1-b-2
80};
81
82} // namespace construct_at_detail
83
84/// @def arene::base::construct_at
85/// @brief @c std::construct_at back ported from C++20
86/// @tparam T the type to construct
87/// @tparam Args type of arguments used for initialization
88/// @param ptr pointer to the uninitialized storage on which a T object will be constructed
89/// @param args arguments used for initialization
90/// @return a pointer to the initialized object
91/// @pre Only participates in overload resolution if <c>new (std::declval<void*>()) T(std::declval<Args>()...)</c>
92/// is well formed in an unevaluated context.
93/// @post The object located at the returned pointer will have been initalized as if constructed via
94/// @c T(std::forward<Args>(args)...)
95/// @note Unlike @c std::construct_at, constexpr evaluation is not supported as dynamic memory allocation (even using
96/// placement new) is not supported until C++20.
97// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
98// object used in multiple TUs."
99// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
100// object used in multiple TUs."
101ARENE_CPP14_INLINE_VARIABLE(construct_at_detail::do_construct_at, construct_at);
102// parasoft-end-suppress AUTOSAR-M7_3_3-a
103// parasoft-end-suppress CERT_CPP-DCL59-a
104
105// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
106
107} // namespace base
108} // namespace arene
109#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MEMORY_CONSTRUCT_AT_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(construct_at_detail::do_construct_at, construct_at)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10