Path: csiph.com!news.mixmin.net!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: Tue, 15 Nov 2022 16:19:31 -0800 Organization: A noiseless patient Spider Lines: 55 Message-ID: <864juzeooc.fsf@linuxsc.com> References: <861qqkhn71.fsf@linuxsc.com> <9d92c7fe-af63-446a-8ad1-81e5e28ef8e7n@googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="c33488f0e6c0635625a64620cc1d5893"; logging-data="2280928"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18rSYGhezxBYW28bn76P8O1nKZPWg9MBUw=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:gbTlTtfl9ZB+tEbMPjMyCGKaJMY= sha1:MuRSRP/XcKVMPDSSf0x1/5Qfw6w= Xref: csiph.com comp.lang.c++:87405 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. Moreover the classification holds regardless of how an implementation might treat constructs that are so classified.