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

|
Related Articles |
|
|