Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't reinterpret_cast convert 'unsigned char' to 'char'?

I am trying to compile this library using MSVC10, and this function is giving me headache:

/*! \brief Read bytes from a \c std::istream
    \param is The stream to be read.
    \param data A pointer to a location to store the bytes.
    \param size The number of bytes to be read.
*/
void _read(std::istream &is, unsigned char *data, int size)
{
    for (int i=0; i < size ; ++i )
      is.get(static_cast<char>(data[i]));
}

error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem &)' : cannot convert parameter 1 from 'char' to 'char &'

The original used static_cast, so I try with reinterpret_cast as suggested elsewhere but that fails too:

error C2440: 'reinterpret_cast' : cannot convert from 'unsigned char' to 'char'

This library comes with unix makefiles. What is the best way to resolve this compile error?

like image 491
Dženan Avatar asked Dec 06 '22 09:12

Dženan


1 Answers

Because reinterpret_cast does not work that way, by definition.

In order to perform memory re-interpretation, you have to apply reinterpret_cast to pointers or references. If you want to reinterpret unsigned char data as char data, you actually have to convert to char & type, not to char type.

In your case that would be

is.get(reinterpret_cast<char &>(data[i]));

Or you can go the pointer route and do

is.get(*reinterpret_cast<char *>(&data[i]));

(which is the same thing).

like image 51
AnT Avatar answered Dec 09 '22 14:12

AnT