reinterpret or static_cast?



 DEVELOP > c-Plus-Plus > reinterpret or static_cast?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Frank A. Uepping"
Date: 19 Nov 2003 03:13:40 PM
Object: reinterpret or static_cast?
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
.

User: "Ron Natalie"

Title: Re: reinterpret or static_cast? 19 Nov 2003 03:29:12 PM
"Frank A. Uepping" <null.0.fau@spamgourmet.com> wrote in message news:bpgme4$qpv$08$1@news.t-online.com...

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.
.

User: "Andrey Tarasevich"

Title: Re: reinterpret or static_cast? 19 Nov 2003 05:26:58 PM
Frank A. Uepping wrote:

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
data = static_cast<const char*>(data) + n;

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?
...

--
Best regards,
Andrey Tarasevich
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER