Re: const Casting

From: Ali R. (nospam_at_company.com)
Date: 11/15/03


Date: Fri, 14 Nov 2003 23:29:58 GMT


"Gene Wirchenko" <gwirchenkoEXCEPT@CAPITALSwencomine.com> wrote in message
news:b8karvsfcmvkkvrev3kshkmov5csand7v9@4ax.com...
> On Fri, 14 Nov 2003 20:43:19 GMT, "Ali R." <nospam@company.com> wrote:
>
> >> void AppendFixed(
> >> std::string & l_b_dest, // IN,OUT: destination buffer
> >> const void * lp_src, // IN: source data pointer
> >> const int & l_s_src // IN: source data length
> >> )
> >>
> >> {
> >> l_b_dest.append(
> >> reinterpret_cast<char *>(const_cast<void *>(lp_src)),l_s_src);
> >
> >append takes a const argument anyway
>
> That adds to my puzzle.
>
    I don't understand the puzzle. Append needs a const char *, so you have
to cast your const void * to a const char *.
    Pretend that the const weren't there. You would still have to convert
from a void * to a const *.
    And it has nothing to do with calling it from the second function.

> >so do this instead
> >
> > l_b_dest.append(reinterpret_cast<const char *>(lp_src),l_s_src);
>
> Yes, but why is my code wrong without the const cast? lp_src
> already is const.
>
> Maybe, this is the crux of the point: Does constness mean that I
> can not cast the data item?
>
> I am trying avoid const casts as I think they are potentially
> very dangerous, at least to my debugging, if not far worse.
> (Reinterpret casts are bad enough.)
>
> [snip]
>
> Sincerely,
>
> Gene Wirchenko
>



Relevant Pages

  • Re: Anders Hejlsberg comment on immutable objects
    ... >> all references are implicitly const is in fact immutable. ... >cast it away, and as I've said many times, everyone, me and you included, ... Implicit conversions are quite different. ... >> the vtable pointer from the instance to the reference. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Anders Hejlsberg comment on immutable objects
    ... Implicitly const classes are immutable by implementation. ... >>cast it away, and as I've said many times, everyone, me and you included, ... > Implicit conversions are quite different. ... I should say immutability should be a design goal for value types. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] Make hash.h usable in the kernel
    ... course for the unsafe cast. ... since allowing this is equivalent to allowing casting away of const. ... Maybe gcc is allowing a loophole or just making no more effort than the ... was only the warning for that cast which made hash.h unusable (the original ...
    (freebsd-net)
  • Re: Anders Hejlsberg comment on immutable objects
    ... >object immutable as either a sideeffect or a matter of const implementation. ... The immutability could be accomplished ... >> look the other way while I cheat, by applying a cast, I expect it to ... >are apparent and a coder can deal with them, an implicit conversion is a ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: const int * assignment
    ... >>> Because here you've overridden the nature of what ptr1 points to. ... I am confused about this cast type for const ... Now it just a pointer to int, ...
    (comp.lang.c)