binary I/O

  • Thread starter vw at iep dot tu-graz dot ac dot at
  • Start date
V

vw at iep dot tu-graz dot ac dot at

Hello

Is there something more elegant in C++ to read/write data
binary into a stream than for example:

out.write(reinterpret_cast<const char *>(&long_var), sizeof(long_var));
in.read(reinterpret_cast<char *>(&unsigned_var), sizeof(unsigned_var));

The need for reinterpret_cast<> looks suspicious.

Thanks
Volker
 
W

WW

vw at iep dot tu-graz dot ac dot at" <"vw at iep dot tu-graz dot ac dot
at said:
Hello

Is there something more elegant in C++ to read/write data
binary into a stream than for example:

out.write(reinterpret_cast<const char *>(&long_var),
sizeof(long_var)); in.read(reinterpret_cast<char *>(&unsigned_var),
sizeof(unsigned_var));

The need for reinterpret_cast<> looks suspicious.

Yes. Use static_cast. *Never* use reinterpret_cast, unless you are doing
something deliberately non-portable.
 
K

Kevin Goodsell

WW said:
vw at iep dot tu-graz dot ac dot at" <"vw at iep dot tu-graz dot ac dot



Yes. Use static_cast. *Never* use reinterpret_cast, unless you are doing
something deliberately non-portable.

You can't static_cast from long* to const char* directly, though. You'd
have to use double static_casts, right?

static_cast<const char *>(static_cast<void *>(&long_var))

Or I suppose you could define a new type of cast:

template<class D, class S>
D safe_reinterpret(const S &source)
{
return static_cast<D>(static_cast<void *>(source));
}

OK, that's rather ugly, and not very safe at all, but it might be a good
solution with some more work.

-Kevin
 
G

Gianni Mariani

Kevin Goodsell wrote:
....
Or I suppose you could define a new type of cast:

template<class D, class S>
D safe_reinterpret(const S &source)

D terribly_unsafe_reinterpret(const S &source)

bad ... bad bad :=)
{
return static_cast<D>(static_cast<void *>(source));
}

OK, that's rather ugly, and not very safe at all, but it might be a good
solution with some more work.

What kind of work ?

Portability in this case is certainly an issue, it has all the problems
associated with writing binary files - see the post I wrote 7 days ago.

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&[email protected]


G
 
K

Kevin Goodsell

Gianni said:
Kevin Goodsell wrote:
...



D terribly_unsafe_reinterpret(const S &source)

bad ... bad bad :=)



What kind of work ?

Well... if you could make sure the destination type is void*, char*,
unsigned char*, or signed char* (or a const and/or volatile qualified
variant of one of those), and the source type is a pointer, then it
should be safe, I think. Don't know how you'd do that, though.

But even as it is I don't think it's less safe than a reinterpret_cast
(other than the fact that it misrepresents itself as being safe).

-Kevin
 
G

Gianni Mariani

Kevin said:
Well... if you could make sure the destination type is void*, char*,
unsigned char*, or signed char* (or a const and/or volatile qualified
variant of one of those), and the source type is a pointer, then it
should be safe, I think. Don't know how you'd do that, though.

OK - that's a good idea. I should probably do that for the network
order template.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top