small fixes to as_bytes() for pod types

- consistent enable_if clause between as_bytes and as_writable_bytes
- Explicitly check for non-const as_writeable_bytes
- move overloads to right section of the file
This commit is contained in:
MikeGitb 2016-12-08 15:52:47 +01:00
parent 8def2618cb
commit 7f2645a4d9

View File

@ -632,6 +632,22 @@ as_writeable_bytes(span<ElementType, Extent> s) noexcept
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
}
template <class T, class = std::enable_if_t<std::is_pod<T>::value>>
span<const byte, sizeof(T)>
as_bytes(const T& e) noexcept
{
return {reinterpret_cast<const byte*>(&e), sizeof(e)};
}
template <class T, class = std::enable_if_t<std::is_pod<T>::value>>
span<byte, sizeof(T)>
as_writeable_bytes(T& e) noexcept
{
static_assert(!std::is_const<T>::value,"Can't create a span of writeable bytes over a const object");
return {reinterpret_cast<byte*>(&e), sizeof(e)};
}
//
// make_span() - Utility functions for creating spans
//
@ -670,19 +686,6 @@ span<typename Ptr::element_type>
make_span(Ptr& cont)
{ return span<typename Ptr::element_type>(cont); }
template <class T, class = std::enable_if_t<std::is_pod<T>::value>>
span<const byte, sizeof(T)>
as_bytes(const T& e) noexcept
{
return{ reinterpret_cast<const byte*>(&e),sizeof(T) };
}
template <class T, class = std::enable_if_t<std::is_trivially_copyable<T>::value>>
span<byte, sizeof(T)>
as_writeable_bytes(T& e) noexcept
{
return{ reinterpret_cast<byte*>(&e),sizeof(T) };
}
// Specialization of gsl::at for span
template <class ElementType, std::ptrdiff_t Extent>