Search code examples
c++reinterpret-cast

Replacing reinterpret_cast with better alternatives?


I have a few places in my project where I use reinterpret_cast to read/write an unsigned integer from a stream. Consider the following functions:

size_t ReadSize(std::stringstream& stream) {
  char buf[sizeof(size_t)];
  stream.read(buf, sizeof(size_t));
  return *(reinterpret_cast<size_t*>(buf));
}

void WriteSize(std::stringstream& stream, size_t n) {
  stream.write(reinterpret_cast<char*>(&n), sizeof(size_t));
}

I started to feel a bit uncomfortable about using reinterpret_cast, even though I've had no problems with it, so I'm wondering, is there a better alternative to this? Assuming I just have 4 bytes in the stream that are supposed to represent this integer.

static_cast isn't applicable here either, I think. Any tips?

P.S. I am not currently concerned about portability or other platform-specific problems that may arise from using reinterpet_cast. I am writing this for a Windows machine.


Solution

  • While the read (and write) functions are specified to take a char*, you don't actually have to pass an array of characters, just cast a pointer to the actual variable right there in the read (or write) call instead:

    std::size_t size;
    if (stream.read(reinterpret_cast<char*>(&size), sizeof(size_t)))
        return size;
    return 0;  // Or something else on error
    

    On an unrelated note, I recommend you change the stream parameter to a std::istream reference instead, then you can use the function for any input stream.