tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
to_address.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_TO_ADDRESS_HPP
5#define TETL_MEMORY_TO_ADDRESS_HPP
6
7#include <etl/_memory/pointer_traits.hpp>
8#include <etl/_type_traits/is_function.hpp>
9
10namespace etl {
11
12/// \brief Obtain the address represented by p without forming a reference to
13/// the object pointed to by p.
14///
15/// \details Fancy pointer overload: If the expression
16/// pointer_traits<Ptr>::to_address(p) is well-formed, returns the result of
17/// that expression. Otherwise, returns to_address(p.operator->()).
18template <typename Ptr>
19constexpr auto to_address(Ptr const& ptr) noexcept
20{
21 if constexpr (requires { pointer_traits<Ptr>::to_address(ptr); }) {
22 return pointer_traits<Ptr>::to_address(ptr);
23 } else {
24 return to_address(ptr.operator->());
25 }
26}
27
28/// \brief Obtain the address represented by p without forming a reference to
29/// the object pointed to by p.
30///
31/// \details Raw pointer overload: If T is a function type, the program is
32/// ill-formed. Otherwise, returns p unmodified.
33template <typename T>
34 requires(not is_function_v<T>)
35constexpr auto to_address(T* ptr) noexcept -> T*
36{
37 return ptr;
38}
39
40} // namespace etl
41
42#endif // TETL_MEMORY_TO_ADDRESS_HPP
Definition adjacent_find.hpp:9
constexpr auto to_address(T *ptr) noexcept -> T *
Obtain the address represented by p without forming a reference to the object pointed to by p.
Definition to_address.hpp:35
constexpr auto to_address(Ptr const &ptr) noexcept
Obtain the address represented by p without forming a reference to the object pointed to by p.
Definition to_address.hpp:19
The pointer_traits class template provides the standardized way to access certain properties of point...
Definition pointer_traits.hpp:18