Path: csiph.com!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: why use static_cast ? Date: Sun, 20 Nov 2022 05:59:34 -0800 Organization: A noiseless patient Spider Lines: 64 Message-ID: <86y1s5afqx.fsf@linuxsc.com> References: <861qqkhn71.fsf@linuxsc.com> <9d92c7fe-af63-446a-8ad1-81e5e28ef8e7n@googlegroups.com> <864juzeooc.fsf@linuxsc.com> <4461edc9-4bc8-43c6-a614-6e5b5c1fa397n@googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="a724f5b4ee1d34e760f89c516bf20c9c"; logging-data="3707309"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18HtamgS8OuRwxSRLjx7MNwoA23/L9tSmM=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:lfmMzLEoieTpx/cO6ti1U12IOlY= sha1:vnOgslmQnE33fO16N4aM73OlQu0= Xref: csiph.com comp.lang.c++:87489 Tiib writes: > On Wednesday, 16 November 2022 at 02:19:46 UTC+2, Tim Rentsch wrote: > >> Tiib writes: >> >>> On Thursday, 3 November 2022 at 02:54:58 UTC+2, Tim Rentsch wrote: >>> >>>> Manfred writes: >>>> >>>>> On 10/20/2022 8:21 PM, Lynn McGuire wrote: >>>>> >>>>>> On 10/20/2022 8:55 AM, Scott Lurndal wrote: >>>>>> >>>>>>> Lynn McGuire writes: >>>>>>> >>>>>>>> I put the following cast into my code and one of my >>>>>>>> programmers wants me to use static cast. Why ? >>>>>>>> https://en.cppreference.com/w/cpp/language/static_cast >>>>>>>> >>>>>>>> longint nfac [8]; >>>>>>>> fem::str <8> * nfac_str = (fem::str <8> *) nfac; >>>>>>>> >>>>>>>> fem::str <8> is an 8 character object that acts like a >>>>>>>> Fortran character*8. longint is a long long. >>>>>>> >>>>>>> Young C++ programmers don't understand C, so they think >>>>>>> that static_cast*> is more "readable". Heh. >>>>>> >>>>>> The programmer in question is 39 and has been writing C and C++ >>>>>> since he was 13 or 14. >>>>>> >>>>>> His actual comment was that static_cast's are easier to grep >>>>>> which I find hard to believe. >>>>> >>>>> If by grep you mean grep(1), then yes, static_cast is definitely >>>>> easier to grep than a C-style cast. However, the advantage of >>>>> reinterpret_cast (static_cast wouldn't work) is not that it is >>>>> more readable. It is that it hurts the eye. >>>>> >>>>> Depending on which compiler you use, I'd verify that what you >>>>> intend to do actually works. As far as the standard is >>>>> concerned, at least one necessary condition is that fem::str<8> >>>>> is effectively an unsigned char[8]. >>>>> Otherwise it's UB unless your compiler says otherwise (although >>>>> it probably Just Works on any of them, in practice.) >>>> >>>> If a construct is described by the standard as being undefined >>>> behavior, said construct is still undefined behavior no matter >>>> what the implementation does. The condition of being undefined >>>> behavior is only a statement of what is mandated (or not) by >>>> the standard; it has nothing to do with whether something >>>> "defines" the behavior. >>> >>> The effect of undefined behavior [...] >> >> Undefined behavior is a classification; it doesn't have effects. > > You managed to read 5 words? > I meant [...] If you can't be bothered to say what you mean in the first place, don't expect people to waste their time trying to figure out what you do mean.