Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What compiler are you using? A static cast from a base type to a derived type might result in an adjustment to the pointer - especially likely if multiple inheritance is involved (which doesn't seem to be the case in your situation from your description). However, it's still possible without MI.</p> <p>The standard indicates that if a null pointer value is being cast that the result will be a null pointer value (5.2.9/8 Static cast). However, I think that on many compilers most downcasts (especially when single inheritance is involved) don't result in a pointer adjustment, so I could imagine that a compiler might have a bug such that it wouldn't make the special check for null that would be required to avoid 'converting' a zero value null pointer to some non-zero value senseless pointer. I would assume that for such a bug to exist you must be doing something unusual to get the compiler to have to adjust the pointer in the downcast.</p> <p>It might be interesting to see what kind of assembly code was generated for your example.</p> <p>And for detailed information about how a compiler might layout an object that might need pointer adjustment with static casts, <a href="http://www.informit.com/store/product.aspx?isbn=0201834545" rel="nofollow noreferrer">Stan Lippman's "Inside the C++ Object Model"</a> is a great resource.</p> <p><a href="http://www.usenix.org/publications/compsystems/1989/fall_stroustrup.pdf" rel="nofollow noreferrer">Stroustrup's paper on Multiple Inheritance for C++</a> (from 1989) is also a good read. It's too bad if a C++ compiler has a bug like I speculate about here - Stroustrup discusses the null pointer issue explicitly in that paper (4.5 Zero Valued Pointers).</p> <p>For your second question:</p> <blockquote> <p>Q2. Is static_casting from B to C/D/E valid?</p> </blockquote> <p>This is perfectly valid as long as when you perform the cast of the B pointer to a C/D/E pointer the B pointer is actually pointing to the B sub-object of a C/D/E object (respectively) and B isn't a virtual base. This is mentioned in the same paragraph of the standard (5.2.9/8 Static cast). I've highlighted the sentences of the paragraph most relevant to your questions:</p> <blockquote> <p>An rvalue of type “pointer to cv1 B”, where B is a class type, can be converted to an rvalue of type “pointer to cv2 D”, where D is a class derived (clause 10) from B, if a valid standard conversion from “pointer to D” to “pointer to B” exists (4.10), cv2 is the same cv-qualification as, or greater cv-qualification than, cv1, and B is not a virtual base class of D. <strong>The null pointer value (4.10) is converted to the null pointer value of the destination type. If the rvalue of type “pointer to cv1 B” points to a B that is actually a sub-object of an object of type D, the resulting pointer points to the enclosing object of type D.</strong> Otherwise, the result of the cast is undefined.</p> </blockquote> <p>As a final aside, you can workaround the problem using something like:</p> <pre><code>Set1(pEntity ? static_cast&lt;C*&gt;(pEntity) : 0); </code></pre> <p>which is what the compiler should be doing for you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload