Enhanced C++03 compatibility

This commit is contained in:
John Wellbelove 2023-05-02 08:09:06 +01:00
parent 40c4fc7c10
commit b7aa13ad19
2 changed files with 17 additions and 14 deletions

View File

@ -947,22 +947,25 @@ namespace etl
#endif
//*************************************************************************
/// Obtains a view to the object representation of the elements of the span s.
/// Obtains a view to the byte representation of the elements of the span s.
//*************************************************************************
template <class T, std::size_t N>
auto as_bytes(span<T, N> s) ETL_NOEXCEPT
template <class T, size_t N>
span<const byte, (N == etl::dynamic_extent) ? (etl::dynamic_extent) : (N * sizeof(T))>
as_bytes(span<T, N> s) ETL_NOEXCEPT
{
return span <const byte, (N == etl::dynamic_extent) ? (etl::dynamic_extent) : (N * sizeof(T)) > {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
return span<const byte, (N == etl::dynamic_extent) ? (etl::dynamic_extent) : (N * sizeof(T))>(reinterpret_cast<byte*>(s.data()), s.size_bytes());
}
template <class T, std::size_t N>
auto as_writable_bytes(span<T, N> s) ETL_NOEXCEPT
//*************************************************************************
/// Obtains a view to the byte representation of the elements of the span s.
//*************************************************************************
template <class T, size_t N>
span<byte, (N == etl::dynamic_extent) ? (etl::dynamic_extent) : (N * sizeof(T))>
as_writable_bytes(span<T, N> s) ETL_NOEXCEPT
{
ETL_STATIC_ASSERT(not etl::is_const<T>::value, "span<T> must be of non-const type");
return span <byte, (N == etl::dynamic_extent) ? (etl::dynamic_extent) : (N * sizeof(T)) > {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
ETL_STATIC_ASSERT(!etl::is_const<T>::value, "span<T> must be of non-const type");
return span<byte, (N == etl::dynamic_extent) ? (etl::dynamic_extent) : (N * sizeof(T))>(reinterpret_cast<byte*>(s.data()), s.size_bytes());
}
}
#endif

View File

@ -1128,13 +1128,13 @@ namespace
//*************************************************************************
TEST(test_convert_span_any_to_span_byte)
{
/* mutable */ float data[2]{3.141592f, 2.71828f };
float data[2]{3.141592f, 2.71828f };
auto const const_bytes = etl::as_bytes(etl::span<float, etl::dynamic_extent>{data});
auto const const_bytes = etl::as_bytes(etl::span<float, etl::dynamic_extent>{data});
auto const writable_bytes = etl::as_writable_bytes(etl::span<float, etl::dynamic_extent>{data});
CHECK_TRUE(const_bytes.size() == sizeof(data));
CHECK_TRUE(writable_bytes.size() == sizeof(data));
CHECK_EQUAL(const_bytes.size(), sizeof(data));
CHECK_EQUAL(writable_bytes.size(), sizeof(data));
}