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
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"
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 // parasoft-begin-suppress CERT_CPP-MEM57-a "False positive: This placement new selects the non-allocating form of
52 // operator 'new', which allocates nothing. Providing storage that is suitably aligned for T is the caller's
53 // responsibility"
54 /// @brief Do the construction
55 /// @tparam T The type to construct
56 /// @tparam Args The types of the arguments
57 /// @param ptr The pointer to the storage
58 /// @param args The arguments to pass
59 /// @return A pointer to the newly constructed object
60 /// @see arene::base::construct_at
61 /// @throws Any exception thrown by the constructor of @c T
62 template <class T, class... Args, constraints<std::enable_if_t<is_constructible<T, Args...>>> = nullptr>
63 auto operator()(T* ptr, Args&&... args) const noexcept(std::is_nothrow_constructible<T, Args...>::value) -> T* {
64 // parasoft-begin-suppress AUTOSAR-A5_2_3-a-2 "False positive: This is creating a new object, it only removes
65 // qualifiers from the storage, which are used to determine the qualifiers on the new object"
66 // parasoft-begin-suppress CERT_CPP-EXP55-a-2 "False positive: This is creating a new object, it only removes
67 // qualifiers from the storage, which are used to determine the qualifiers on the new object"
68 // Justification: False positive about newly created resource
69 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory,cppcoreguidelines-pro-type-const-cast)
70 return ::new (const_cast<void*>(static_cast<give_qualifiers_to<T, void>*>(ptr))) T(std::forward<Args>(args)...);
71 // parasoft-end-suppress AUTOSAR-A5_2_3-a-2
72 // parasoft-end-suppress CERT_CPP-EXP55-a-2
73 }
74 // parasoft-end-suppress CERT_CPP-MEM57-a
75 // parasoft-end-suppress CERT_CPP-ERR55-a-2
76 // parasoft-end-suppress CERT_CPP-ERR50-g-3
77 // parasoft-end-suppress CERT_CPP-ERR50-h-3
78 // parasoft-end-suppress CERT_CPP-ERR51-b-3
79 // parasoft-end-suppress AUTOSAR-A15_5_3-h-2
80 // parasoft-end-suppress AUTOSAR-A15_5_3-g-2
81 // parasoft-end-suppress AUTOSAR-A15_3_2-a-2
82 // parasoft-end-suppress AUTOSAR-M15_3_4-b-2
83 // parasoft-end-suppress AUTOSAR-A15_2_1-b-2
84};
85
86} // namespace construct_at_detail
87
88/// @brief @c std::construct_at back ported from C++20
89/// @tparam T the type to construct
90/// @tparam Args type of arguments used for initialization
91/// Pass uninitialized storage as @c ptr and construction arguments as @c args.
92/// @return a pointer to the initialized object
93/// @pre Only participates in overload resolution if <c>new (std::declval<void*>()) T(std::declval<Args>()...)</c>
94/// is well formed in an unevaluated context.
95/// @post The object located at the returned pointer will have been initalized as if constructed via
96/// @c T(std::forward<Args>(args)...)
97/// @note Unlike @c std::construct_at, constexpr evaluation is not supported as dynamic memory allocation (even using
98/// placement new) is not supported until C++20.
99// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
100// object used in multiple TUs."
101// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
102// object used in multiple TUs."
103ARENE_CPP14_INLINE_VARIABLE(construct_at_detail::do_construct_at, construct_at);
104// parasoft-end-suppress AUTOSAR-M7_3_3-a
105// parasoft-end-suppress CERT_CPP-DCL59-a
106
107// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
108
109} // namespace base
110} // namespace arene
111#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MEMORY_CONSTRUCT_AT_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition accumulate.hpp:37
constexpr bool substitution_succeeds
Determines if a template can be instantiated successfully, similar to C++20's requires clause.
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
typename static_if< give_qualifiers_to_detail::is_volatile_v< Source > >::template then_apply_else_apply< give_qualifiers_to_detail::give_qualifiers_to_impl_volatile, give_qualifiers_to_detail::give_qualifiers_to_impl_nonvolatile, Source, Target > give_qualifiers_to
For a type Source and type Target: if Source is const volatile results in Target const volatile other...
Definition give_qualifiers_to.hpp:68
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,...
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24