How to derive continuous memory from an iterator

Be that as it may, the built-in algorithm stl::copy()in VC ++ (Dinkumware) shows what it can use memcpy()for data that can be trivially copied. Is it possible for a mere mortal? - assuming each element is_trivially_copyable.

Does random_access_iterator use continuous memory? The standard is not clear to me.

So, if all that you have in the template is an iterator or two, is it possible to deduce during compilation that the base array can be copied using memcpy(), and if so, how?

EDIT is my motivation. I have a procedure that moves a data block rather than copying it. To get speed when the data is memmove-capable, I sometimes called stl :: copy. I was wondering if this is the only way. (As a child, I always tried at home.)

// Move a range of values
template<class Ptr, class Ptr2>
inline Ptr2 move(Ptr src, Ptr end, Ptr2 dest) {
    using value_type = std::iterator_traits<Ptr>::value_type;
    if constexpr (std::is_trivially_copyable_v<value_type>) {
        return std::copy(src, end, dest);
    } else {
        while (src != end) {
            *dest = std::move(*src);
            ++src; ++dest;
        }
    }
    return dest;
}

EDIT: Kudos to zett42 for finding this related question: Continuous detection of an iterator. I cannot explain how I missed it.

EDIT MORE: After going down many winding little passages, I found that Dinkum uses secret tags for iterators that are inside with a crowd, for example. _Really_trivial_ptr_iterator_tag. Therefore, the prospects look dull.

My $0.02 worth: , iterator_category , , "points_into_contiguous_memory" ..... random_access_iterator - ad-hoc-, , . , . , , .

.

+3
2

A) . , F ( +/- n) = iterator.next/prev.....n. . ( )

B) . . , , , 2 .

?, . , , . , , , memmove memcpy .

, , , .

+1

random_access_iterator ?

: .

: .

: std::vector ( ) , , data().

std::deque ( ) , (std::deque , / ), , .

+2

Source: https://habr.com/ru/post/1690485/


All Articles