Fix etl::as_bytes for etl::span<const T> (#1266)

A missing 'const' in the etl::as_bytes implementation was causing a
compile-time error when etl::as_bytes was called on a span of const
values.

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
This commit is contained in:
taltenbach 2026-01-17 23:04:52 +01:00 committed by GitHub
parent c92dbc2fce
commit 55503e0b97
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 6 additions and 4 deletions

View File

@ -1303,7 +1303,7 @@ namespace etl
span<const byte, (Size == etl::dynamic_extent) ? (etl::dynamic_extent) : (Size * sizeof(T))>
as_bytes(span<T, Size> s) ETL_NOEXCEPT
{
return span<const byte, (Size == etl::dynamic_extent) ? (etl::dynamic_extent) : (Size * sizeof(T))>(reinterpret_cast<byte*>(s.data()), s.size_bytes());
return span<const byte, (Size == etl::dynamic_extent) ? (etl::dynamic_extent) : (Size * sizeof(T))>(reinterpret_cast<const byte*>(s.data()), s.size_bytes());
}
//*************************************************************************

View File

@ -1325,8 +1325,9 @@ namespace
TEST(test_convert_span_any_to_span_byte)
{
float data[2]{3.141592f, 2.71828f };
const float const_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<const float, etl::dynamic_extent>{const_data});
auto const writable_bytes = etl::as_writable_bytes(etl::span<float, etl::dynamic_extent>{data});
CHECK_EQUAL(const_bytes.size(), sizeof(data));

View File

@ -1163,12 +1163,13 @@ namespace
TEST(test_convert_span_any_to_span_byte)
{
float data[2]{3.141592f, 2.71828f};
const float const_data[2]{3.141592f, 2.71828f};
#if ETL_USING_CPP17
auto const const_bytes = etl::as_bytes(etl::span{ data });
auto const const_bytes = etl::as_bytes(etl::span{ const_data });
auto const writable_bytes = etl::as_writable_bytes(etl::span{ data });
#else
auto const const_bytes = etl::as_bytes(etl::span<float, 2>(data));
auto const const_bytes = etl::as_bytes(etl::span<const float, 2>(const_data));
auto const writable_bytes = etl::as_writable_bytes(etl::span<float, 2>(data));
#endif