tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
pointer_traits.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_MEMORY_POINTER_TRAITS_HPP
5#define TETL_MEMORY_POINTER_TRAITS_HPP
6
7#include <etl/_cstddef/ptrdiff_t.hpp>
8#include <etl/_memory/addressof.hpp>
9#include <etl/_type_traits/is_void.hpp>
10
11namespace etl {
12
13/// The pointer_traits class template provides the standardized way to
14/// access certain properties of pointer-like types.
15///
16/// https://en.cppreference.com/w/cpp/memory/pointer_traits
17template <typename Ptr>
19 using pointer = Ptr;
20 using element_type = typename Ptr::element_type;
21 using difference_type = typename Ptr::difference_type;
22
23 /// Constructs a dereferenceable pointer or pointer-like object
24 /// ("fancy pointer") to its argument.
25 ///
26 /// \details
27 /// https://en.cppreference.com/w/cpp/memory/pointer_traits/pointer_to
28 ///
29 /// \param r Reference to an object of type element_type&.
30 /// \returns A pointer to r, of the type pointer_traits::pointer.
31 [[nodiscard]] static auto pointer_to(element_type& r) -> pointer
32 {
33 return Ptr::pointer_to(r);
34 }
35};
36
37/// The pointer_traits class template provides the standardized way to
38/// access certain properties of pointer-like types.
39///
40/// https://en.cppreference.com/w/cpp/memory/pointer_traits
41///
42/// \tparam T A raw pointer
43template <typename T>
44struct pointer_traits<T*> {
45 using pointer = T*;
46 using element_type = T;
47 using difference_type = etl::ptrdiff_t;
48 template <typename U>
49 using rebind = U*;
50
51 /// Constructs a dereferenceable pointer or pointer-like object
52 /// ("fancy pointer") to its argument.
53 ///
54 /// https://en.cppreference.com/w/cpp/memory/pointer_traits/pointer_to
55 ///
56 /// \param r Reference to an object of type element_type&.
57 /// \returns A pointer to r, of the type pointer_traits::pointer.
58 [[nodiscard]] static auto pointer_to(T& r) -> pointer
59 requires(not etl::is_void_v<T>)
60 {
61 return etl::addressof(r);
62 }
63};
64
65} // namespace etl
66
67#endif // TETL_MEMORY_POINTER_TRAITS_HPP
Definition adjacent_find.hpp:9
static auto pointer_to(T &r) -> pointer requires(not etl::is_void_v< T >)
Constructs a dereferenceable pointer or pointer-like object ("fancy pointer") to its argument.
Definition pointer_traits.hpp:58
The pointer_traits class template provides the standardized way to access certain properties of point...
Definition pointer_traits.hpp:18
static auto pointer_to(element_type &r) -> pointer
Constructs a dereferenceable pointer or pointer-like object ("fancy pointer") to its argument.
Definition pointer_traits.hpp:31