reinterpret or static_cast?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank A. Uepping

    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<con st char*>(data) += n; // reinterpret_cas t?
    size -= n;
    }
    }

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

    /FAU


  • Ron Natalie

    #2
    Re: reinterpret or static_cast?


    "Frank A. Uepping" <null.0.fau@spa mgourmet.com> wrote in message news:bpgme4$qpv $08$1@news.t-online.com...
    [color=blue]
    > Here it is necessary to do a cast in order to do some pointer arithmetic.
    > Is the static_cast or the reinterpret_cas t appropriate?[/color]

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


    Comment

    • Andrey Tarasevich

      #3
      Re: reinterpret or static_cast?

      Frank A. Uepping wrote:[color=blue]
      > 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<con st char*>(data) += n; // reinterpret_cas t?[/color]

      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<con st 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<con st char*>(data) + n;

      [color=blue]
      > size -= n;
      > }
      > }
      >
      > Here it is necessary to do a cast in order to do some pointer arithmetic.
      > Is the static_cast or the reinterpret_cas t appropriate?
      > ...[/color]

      --
      Best regards,
      Andrey Tarasevich

      Comment

      Working...