Added futurecpp.h

This commit is contained in:
2021-07-17 18:09:53 +02:00
parent 3dc1efd38d
commit 3a350f1189
2 changed files with 30 additions and 0 deletions

View File

@ -3,6 +3,7 @@ set(headers
src/espcrc32builder.h
src/esprandom.h
src/espstrutils.h
src/futurecpp.h
src/lockhelper.h
src/lockingqueue.h
src/recursivelockhelper.h

29
src/futurecpp.h Normal file
View File

@ -0,0 +1,29 @@
#pragma once
// system includes
#include <cstring>
#include <limits>
// C++20 backports (until espressif finally updates their aged compiler suite)
namespace std {
template <class To, class From>
typename std::enable_if_t<
sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> && std::is_trivially_copyable_v<To>,
To>
// constexpr support needs compiler magic
bit_cast(const From& src) noexcept
{
static_assert(std::is_trivially_constructible_v<To>,
"This implementation additionally requires destination type to be trivially constructible");
To dst;
std::memcpy(&dst, &src, sizeof(To));
return dst;
}
template <typename EnumT, typename = std::enable_if_t<std::is_enum<EnumT>{}>>
constexpr std::underlying_type_t<EnumT> to_underlying(EnumT e) noexcept {
return static_cast<std::underlying_type_t<EnumT>>(e);
}
} // namespace std