tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
construct_at.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_MEMORY_CONSTRUCT_AT_HPP
5#define TETL_MEMORY_CONSTRUCT_AT_HPP
6
7#include <etl/_config/all.hpp>
8
9#include <etl/_new/operator.hpp>
10#include <etl/_type_traits/declval.hpp>
11#include <etl/_utility/forward.hpp>
12
13#if __has_include(<memory>)
14 // TODO: Only include private header that defines construct_at from each STL
15 // STL = https://github.com/microsoft/STL/blob/main/stl/inc/xutility
16 // libc++ = https://github.com/llvm/llvm-project/blob/main/libcxx/include/__memory/construct_at.h
17 // libstdc++ = https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_construct.h
18 #include <memory>
19#else
20// NOLINTBEGIN
21
22namespace std {
23
24template <typename T, typename... Args, typename = decltype(::new (etl::declval<void*>()) T(etl::declval<Args>()...))>
25constexpr auto construct_at(T* p, Args&&... args) -> T*
26{
27 return ::new (static_cast<void*>(p)) T(etl::forward<Args>(args)...);
28}
29
30} // namespace std
31
32// NOLINTEND
33#endif
34
35namespace etl {
36
37/// \brief Creates a T object initialized with arguments args... at given address p.
38template <typename T, typename... Args, typename = decltype(::new (etl::declval<void*>()) T(etl::declval<Args>()...))>
39constexpr auto construct_at(T* p, Args&&... args) -> T*
40{
41 return ::std::construct_at(p, etl::forward<Args>(args)...);
42}
43
44} // namespace etl
45
46#endif // TETL_MEMORY_CONSTRUCT_AT_HPP
Definition adjacent_find.hpp:9
auto declval() noexcept -> add_rvalue_reference_t< T >
constexpr auto construct_at(T *p, Args &&... args) -> T *
Creates a T object initialized with arguments args... at given address p.
Definition construct_at.hpp:39