By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

reinterpret or static_cast?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Frank A. Uepping" <nu********@spamgourmet.com> wrote in message news:bp*************@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.
Jul 22 '05 #2

P: n/a
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

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.