Skip to content

Commit fa65bbf

Browse files
author
MarcoFalke
committed
span: Add BytePtr helper
1 parent 8b5a4de commit fa65bbf

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/span.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,21 @@ T& SpanPopBack(Span<T>& span)
243243
return back;
244244
}
245245

246+
//! Convert a data pointer to a std::byte data pointer.
247+
//! Where possible, please use the safer AsBytes helpers.
248+
inline const std::byte* BytePtr(const void* data) { return reinterpret_cast<const std::byte*>(data); }
249+
inline std::byte* BytePtr(void* data) { return reinterpret_cast<std::byte*>(data); }
250+
246251
// From C++20 as_bytes and as_writeable_bytes
247252
template <typename T>
248253
Span<const std::byte> AsBytes(Span<T> s) noexcept
249254
{
250-
return {reinterpret_cast<const std::byte*>(s.data()), s.size_bytes()};
255+
return {BytePtr(s.data()), s.size_bytes()};
251256
}
252257
template <typename T>
253258
Span<std::byte> AsWritableBytes(Span<T> s) noexcept
254259
{
255-
return {reinterpret_cast<std::byte*>(s.data()), s.size_bytes()};
260+
return {BytePtr(s.data()), s.size_bytes()};
256261
}
257262

258263
template <typename V>

0 commit comments

Comments
 (0)