Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: Strange code-generation Date: Wed, 18 Sep 2019 08:47:52 -0700 Organization: A noiseless patient Spider Lines: 52 Message-ID: <867e654ndj.fsf@linuxsc.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="d7421874ff1696044f351dbc5e408957"; logging-data="28854"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Y8lUlnknE8G7AO8mqsvB570AEx07nsTI=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:SWPD6tdN/3Co2HU0M9CdOUQPaPw= sha1:koUH0cUQH8n/52TfaB18dpWfz1M= Xref: csiph.com comp.lang.c++:68506 Paavo Helde writes: > On 16.09.2019 15:53, Bonita Montero wrote: > >> I just experimented a bit with downcasting to a derived class. >> So here's the code: >> >> struct A >> { >> int a, b, c; >> }; >> >> struct B >> { >> int d, e, f; >> }; >> >> struct D : public A, public B >> { >> int i, j, k; >> }; >> >> D *f( B &b ) >> { >> return &static_cast(b); >> } >> >> This is what you will expect ... with g++: >> >> leaq -12(%rdi), %rax >> ret >> >> This is what makes MSVC from the above code >> >> xor edx, edx >> lea rax, QWORD PTR [rcx-12] >> test rcx, rcx >> cmove rax, rdx >> ret 0 >> >> So MSVC keeps a nullpointer when the input was also a nullpointer. >> Is there any requirement of the standard that mandates this behaviour? >> I think it would be even stupid to include that into the standard. > > In standard C++ one cannot legally construct a "null reference", so > the standard cannot mandate any behavior regarding them. [...] Sure it could. The Standard doesn't say that they can't exist, and even if they couldn't exist the Standard could still specify a behavior for how they must be treated. And it might even be useful to give such a specification, for implementations that choose to provide null references by means of an extension.