reinterpret or static_cast?

F

Frank A. Uepping

Hi,
look at this function, which does some low-level IO.

void writen(int fd, const void* data, size_t size)
{
while (size) {
int n = write(fd, data, size);
if (n < 0)
throw logic_error...

static_cast<const char*>(data) += n; // reinterpret_cast?
size -= n;
}
}

Here it is necessary to do a cast in order to do some pointer arithmetic.
Is the static_cast or the reinterpret_cast appropriate?

/FAU
 
R

Ron Natalie

Frank A. Uepping said:
Here it is necessary to do a cast in order to do some pointer arithmetic.
Is the static_cast or the reinterpret_cast appropriate?

I'd prefer static_cast (in it's undo defined conversions role), but I don't know that
it is more appropriate than reinterpret_cast in this case.
 
A

Andrey Tarasevich

Frank said:
Hi,
look at this function, which does some low-level IO.

void writen(int fd, const void* data, size_t size)
{
while (size) {
int n = write(fd, data, size);
if (n < 0)
throw logic_error...

static_cast<const char*>(data) += n; // reinterpret_cast?

This will not even compile. The result of this cast in an rvalue.
Operator '+=' cannot be applied to an rvalue. Most likely is was
supposed to be

static_cast<const char*&>(data) += n

But I don't think this is a valid technique, regardless of the cast
operator used. The more proper way to do this would be the following
 

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

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,378
Latest member
BlakeLig

Latest Threads

Top