mimic++ v9.2.1
Loading...
Searching...
No Matches
C++20Compatibility.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024 - 2025.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef MIMICPP_UTILITIES_CXX20_COMPATIBILITY_HPP
7#define MIMICPP_UTILITIES_CXX20_COMPATIBILITY_HPP
8
9#pragma once
10
12
13#ifndef MIMICPP_DETAIL_IS_MODULE
14 #include <version>
15#endif
16
17#ifdef __cpp_lib_bit_cast
18
19 #ifndef MIMICPP_DETAIL_IS_MODULE
20 #include <bit>
21 #endif
22
24{
25 using std::bit_cast;
26}
27
28#else
29
30 #ifndef MIMICPP_DETAIL_IS_MODULE
31 #include <cstring>
32 #include <type_traits>
33 #endif
34
36{
37 // taken from: https://www.en.cppreference.com/w/cpp/numeric/bit_cast.html
38 template <typename To, typename From>
39 std::enable_if_t<
40 sizeof(To) == sizeof(From)
41 && std::is_trivially_copyable_v<From>
42 && std::is_trivially_copyable_v<To>,
43 To>
44 // constexpr support needs compiler magic
45 bit_cast(From const& src) noexcept
46 {
47 static_assert(std::is_trivially_constructible_v<To>, "This implementation additionally requires destination type to be trivially constructible");
48
49 To dst;
50 std::memcpy(&dst, &src, sizeof(To));
51
52 return dst;
53 }
54}
55
56#endif
57
58#endif
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
Definition Fwd.hpp:445
std::enable_if_t< sizeof(To)==sizeof(From) &&std::is_trivially_copyable_v< From > &&std::is_trivially_copyable_v< To >, To > bit_cast(From const &src) noexcept
Definition C++20Compatibility.hpp:45