How to check whether iterators form a contiguous memory zone?

Posted by Vincent on Stack Overflow See other posts from Stack Overflow or by Vincent
Published on 2012-09-30T03:28:33Z Indexed on 2012/09/30 3:37 UTC
Read the original article Hit count: 140

Filed under:
|
|
|
|

I currently have the following function to read an array or a vector of raw data (_readStream is a std::ifstream) :

template<typename IteratorType> 
inline bool MyClass::readRawData(
    const IteratorType& first, 
    const IteratorType& last, 
    typename std::iterator_traits<IteratorType>::iterator_category* = nullptr
    )
{
    _readStream.read(reinterpret_cast<char*>(&*first), (last-first)*sizeof(*first));
    return _readStream.good();
}

First question : does this function seem ok for you ?

As we read directly a block of memory, it will only work if the memory block from first to last is contiguous in memory. How to check that ?

© Stack Overflow or respective owner

Related posts about c++

Related posts about file