Utility Rack
Loading...
Searching...
No Matches
cast_ptr_to.hpp File Reference

Utility functions for conveniently casting a pointer between types, primarily from some type T to either a char* or std::byte*. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename Dst , typename Src >
const Dst * chops::cast_ptr_to (const Src *p) noexcept
 
template<typename Dst , typename Src >
Dst * chops::cast_ptr_to (Src *p) noexcept
 

Detailed Description

Utility functions for conveniently casting a pointer between types, primarily from some type T to either a char* or std::byte*.

C++ reinterpret_cast can cast any pointer to another pointer type, but the behaviour can be undefined, depending on the access and usage. C++ guarantees that a pointer can be safely cast to void* and then to a char* (or std::byte*, which is one of the char* types), and that pointer can be cast back to the original pointer type. Casting between two unrelated types (outside of the void* and char* conversions already mentioned) results in undefined behavior (even if functionally the memory layout is the same and execution is successful on a given platform).

Both const and non-const overloads are available.

Note
This utility is mostly or completely outdated due to the C++ 20 std::bit_cast function.
Authors
Thurman Gillespy, Cliff Green

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)