I've this template function
template <typename T, typename It, std::enable_if_t<std::is_integral<T>::value, int> = 0>
inline T decode(It &it) {
static_assert(std::is_same<typename std::iterator_traits<It>::value_type, std::uint8_t>::value, "invalid");
T* v_p = reinterpret_cast<T*>(&*it);
it += sizeof(T);
return *v_p;
}
that is used to decode integers from a raw pointer. The function can be used with any type that has iterator traits, i.e. either pointers to std::uint8_t
or iterators to std::uint8_t
STL containers with iterators that meet LegacyContiguousIterator requirements.
The function works, but I'm not sure about the performance of the call to &*it
when it
is a pointer. The operators are needed to get the pointer from an iterator, as explained in this answer, but it seems overkill for POD pointers. Is the compiler allowed to just drop the operations, or it is better to write a specialization for pointers