view: use SFINAE to diable casts that can't be aligned

This commit is contained in:
Danny Robson 2019-01-18 17:19:39 +11:00
parent b0d29f94e9
commit b9c0716c66

View File

@ -393,10 +393,34 @@ namespace cruft {
///////////////////////////////////////////////////////////////////////
/// Explicitly cast to a view with different iterator types.
///
/// The source and destination iterator types must be:
/// * pointers
/// * alignable
///
/// It is undefined behaviour to cast with begin and/or end iterators
/// that do not have natural alignment. Instrumented builds _may_
/// provide diagnostics or assertions in this case.
///
/// \tparam ValueT The new iterator type
/// \return A view that uses the new iterator type
template <
typename ValueT,
typename = std::enable_if_t<
std::is_pointer_v<BeginT> && std::is_pointer_v<ValueT>
// We can only convert views that use pointer iterators
std::is_pointer_v<BeginT> &&
std::is_pointer_v<EndT> &&
std::is_same_v<BeginT,EndT> &&
std::is_pointer_v<ValueT> &&
// The values they point to must allow for alignment in one
// direction or another.
(
sizeof (typename std::iterator_traits<BeginT>::value_type) %
sizeof (typename std::iterator_traits<ValueT>::value_type) == 0 ||
sizeof (typename std::iterator_traits<ValueT>::value_type) %
sizeof (typename std::iterator_traits<BeginT>::value_type) == 0
)
>
>
view<ValueT>