File tree Expand file tree Collapse file tree 1 file changed +17
-3
lines changed
Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change 88#pragma once
99
1010#include < algorithm>
11+ #include < cstring>
1112#include < deque>
1213#include < vector>
1314
@@ -45,9 +46,22 @@ size_t erase_if(Container& c, const Pred& pred) {
4546 return removed;
4647}
4748
48- template <typename To, typename From>
49- constexpr To bit_cast (const From& from) noexcept {
50- return __builtin_bit_cast (To, from);
49+ template <class To , class From >
50+ std::enable_if_t <
51+ sizeof (To) == sizeof (From) &&
52+ std::is_trivially_copyable_v<From> &&
53+ std::is_trivially_copyable_v<To>,
54+ To>
55+ // constexpr support needs compiler magic
56+ bit_cast (const From& src) noexcept
57+ {
58+ static_assert (std::is_trivially_constructible_v<To>,
59+ " This implementation additionally requires "
60+ " destination type to be trivially constructible" );
61+
62+ To dst;
63+ std::memcpy (&dst, &src, sizeof (To));
64+ return dst;
5165}
5266
5367} // namespace std20
You can’t perform that action at this time.
0 commit comments