mardi 28 mai 2019

what is good solution from template pointer: *(T*) to C++ type casting?

I'd like to convert C style template pointer: *(T*)(&buf[index]) to c++ type casting in bytebuffer code.

code in https://github.com/RamseyK/ByteBufferCpp/blob/master/src/ByteBuffer.hpp#L170

template<typename T> 
T read(uint32_t index) const {
    if (index + sizeof(T) <= buf.size())
        return *((T*) &buf[index]); //C Style Cast
    return 0;
}

My solution: C++ Type cast

return *(reiterpret_cast<T*>(&buf[index])); //C++ Style Cast

Is this good solution?

Or other good solutions with c++11?

Aucun commentaire:

Enregistrer un commentaire