Skip to content

Commit

Permalink
In array_ref, apply constexpr to reinterpret_as when same size elemen…
Browse files Browse the repository at this point in the history
…ts, and use std::data instead of std::begin now that std::string properly supports mutable data
  • Loading branch information
fdwr committed Oct 6, 2022
1 parent 18284d4 commit 96479f9
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions source/Common.ArrayRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ class array_ref
template<typename NewType> array_ref<NewType> reinterpret_as()
{
size_type adjustedByteSize = size_in_bytes();
adjustedByteSize -= adjustedByteSize % sizeof(NewType);
if constexpr (sizeof(NewType) != sizeof(T))
{
adjustedByteSize -= adjustedByteSize % sizeof(NewType);
}
return array_ref<NewType>(
reinterpret_cast<NewType*>(begin_),
reinterpret_cast<NewType*>(to_byte_pointer(begin_) + adjustedByteSize)
Expand Down Expand Up @@ -241,11 +244,11 @@ array_ref<T> make_array_ref(T* begin, T* end)
}

template <typename ContiguousContainer>
auto make_array_ref(ContiguousContainer& container) -> array_ref<typename std::remove_reference<decltype(*std::begin(container))>::type>
auto make_array_ref(ContiguousContainer& container) -> array_ref<typename std::remove_reference<decltype(*std::data(container))>::type>
{
// The remove_reference is necessary because decltype retains the reference
// from std::vector's dereferenced iterator.
using ArrayRefType = typename std::remove_reference<decltype(*std::begin(container))>::type;
using ArrayRefType = typename std::remove_reference<decltype(*std::data(container))>::type;
return array_ref<ArrayRefType>(container);
}

Expand Down

0 comments on commit 96479f9

Please sign in to comment.