4

I'm new to C++ and reading some code as follows:

template<typename T>
std::istream & read(std::istream* stream, T& value){
    return stream->read(reinterpret_cast<char*>(&value), sizeof(T));
}

and call it:

size_t size;
read(&stream, size);

Can anyone explain what is the purpose of reinterpret_cast used here and what is result after read function is invoked?

UPDATE:

The question is:

If the stream contains a string e.g "test", after read is invoked, value's type becomes char * and its content is "test"?

Foredoomed
  • 2,219
  • 2
  • 21
  • 39
  • 1
    Google is your friend. Try `reinterpret_cast c++`, and look at the second and third results. – Ken White May 26 '13 at 05:55
  • 2
    That code snippet appears to be an attempt to write a generic serialization routine. If that's in fact what's going on, the approach shown is quite naïve and dangerous. – In silico May 26 '13 at 05:56
  • @In silico If the stream contains a string e.g "test", after read is invoked, value's type becomes char * and its content is "test"? – Foredoomed May 26 '13 at 06:04
  • @KenWhite the second and third google results may not be identical from user to user, region to region, or day to day. – justin May 26 '13 at 06:04
  • @justin: Good point, but it doesn't change the fact that a basic search would answer this question. – Ken White May 26 '13 at 06:05
  • @KenWhite oh, i agreed with the GIYF bit :) – justin May 26 '13 at 06:05
  • @Foredoomed: Who knows? Whether the code snippet works depends critically on the compiler and/or the platform (the immediately obvious problem I see is the issue of [endianness](http://en.wikipedia.org/wiki/Endianness)). It's not much use to talk about its behavior when `reinterpret_cast` is used in this manner. – In silico May 26 '13 at 06:13
  • @Foredoomed: A general rule with `reinterpret_cast`: if you don't know what it means, then you aren't ready you *use it*. – Nicol Bolas May 26 '13 at 07:09

2 Answers2

2

reinterpret_cast<T>() forces a given bit-pattern to be interpreted as the type you desire. It is the most "brutal" among casts.

From MSDN:

Allows any pointer to be converted into any other pointer type. Also allows any integral >type to be converted into any pointer type and vice versa.

Misuse of the reinterpret_cast operator can easily be unsafe. Unless the desired >conversion is inherently low-level, you should use one of the other cast operators. The reinterpret_cast operator can be used for conversions such as char* to int*, or >One_class* to Unrelated_class*, which are inherently unsafe.

The result of a reinterpret_cast cannot safely be used for anything other than being >cast back to its original type. Other uses are, at best, nonportable.


In you example

template<typename T>
std::istream & read(std::istream* stream, T& value){
    return stream->read(reinterpret_cast<char*>(&value), sizeof(T));
}

it is used to read from a given stream and cast the read data to char* to treat it as a sequence of bytes (assuming char is unsigned by default).

bash.d
  • 13,029
  • 3
  • 29
  • 42
0

The read function simply reads a number of bytes into a buffer, and the reinterpret_cast here turns an arbitrary rvalue into such a buffer by overriding the actual type of the value. If the stream actually did contain a value of the correct type, the result is that this value is stored into value.

microtherion
  • 3,938
  • 1
  • 15
  • 18
  • If the stream contains a string e.g "test", after read is invoked, value's type becomes char * and its content is "test"? – Foredoomed May 26 '13 at 06:04
  • value does **not** change its type, but yes, if the type of value was `char[5]` when calling this function, and the stream contained the 5 bytes `’t’, ‘e’, ’s’, ’t’, ‘\0’`, then value would indeed contain “test” (strings are NOT a good application for this routine, because they are typically not fixed length, and if they are, one wouldn’t write a terminator). – microtherion May 26 '13 at 06:08