3

Consider a class layout :

| A | B | ( class B is derived from A )
0x0 0x8

of course, there is nothing to adjust on downcasting or upcasting. but is behaviour of compiler defined for this case in Standard.?

if not, then, in general, is static_casting of nullptr safe when there is no multiple inheritance.?


A * volatile a_ptr = nullptr ; // or change with B * and cast to A * 
assert( ! static_cast< B * >( a_ptr ) ) ; // is that guaranteed by Standard.? 

Does compiler always ( in all implementations ) do not perform adjustment.?

and more generally (for case of multiple inheritance), can compiler adjust nullptr within static_cast.?


related question, also unanswered.

Community
  • 1
  • 1
Volodymyr Boiko
  • 1,533
  • 15
  • 29
  • Possible duplicate of [When does invoking a member function on a null instance result in undefined behavior?](http://stackoverflow.com/questions/2474018/when-does-invoking-a-member-function-on-a-null-instance-result-in-undefined-beha) – Jonathan Mee Dec 15 '16 at 18:52
  • Is what behavior defined by the standard? – NathanOliver Dec 15 '16 at 18:52
  • 1
    The `static_cast` of a `nullptr` is always undefined behavior in the standard. So no it's definitely not safe. – Jonathan Mee Dec 15 '16 at 18:54
  • 4
    @JonathanMee What? `static_cast` on a `nullptr` is safe. it gives you a `nullptr` of the type you cast to. – NathanOliver Dec 15 '16 at 18:56
  • @NathanOliver Ugh, you're correct, I'm thinking of calling a function on that method. I think you should probably write up that answer. – Jonathan Mee Dec 15 '16 at 18:57
  • See [this question](http://stackoverflow.com/q/40031426/2642059). `nullptr` already is a pointer to `NULL` of the type `A` *and* `B`. No need to cast. – Jonathan Mee Dec 15 '16 at 19:02

1 Answers1

4

static_cast of nullptr is always safe. No matter what is your class layout, you can always static_cast nullptr within class hierarchy and will have defined results - nullptr of the cast type.

Guarantee that any type cast from nullptr will result in nullptr can be found in Standard 5.2.9:

A prvalue of type “pointer to cv1 B,” where B is a class type, can be converted to a prvalue of type “pointerto 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 neither a virtual base class of D nor a base class of a virtual base class of D. The null pointer value (4.10) is converted to the null pointer value of the destination type.

Although nullptr can be implictly converted to any pointer type, you might want an explicit cast in some cases, for example, when dealing with templates.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • But then I'm not sure why you'd want to. The `nullptr` is already a pointer to `NULL` matching any type. – Jonathan Mee Dec 15 '16 at 19:03
  • @JonathanMee, for templates, for example. – SergeyA Dec 15 '16 at 19:04
  • @SergeyA, does static_cast check that passed_ptr != nullptr.? – Volodymyr Boiko Dec 15 '16 at 19:07
  • 2
    @GreenTree if the compiler needs to adjust the pointer to account for layout differences I believe it needs to preserve pointers that are `nullptr`, so whatever that takes. In your example though there's no adjustment necessary, so no check is necessary either. – Mark Ransom Dec 15 '16 at 19:13
  • @Mark Ransom, you are talking about optimizations, but is such check being done in general.? Please post an answer with quotes from the book. – Volodymyr Boiko Dec 15 '16 at 19:45
  • @Mark Ransom, if it doesn't burden you. – Volodymyr Boiko Dec 15 '16 at 19:49
  • @GreenTree the relevant thing is what the standard says. Some people make a habit of reading the standard and others do not - I'm one of those *not* people, but I try to keep myself informed anyway. I'm reasonably certain that the standard says null pointers must be preserved when casting up and down the class hierarchy, but I wouldn't know where to look. Beyond that are implementation details that the standard won't have anything to say about. – Mark Ransom Dec 15 '16 at 19:55
  • @GreenTree, I've given you guarantees you were looking for. – SergeyA Dec 15 '16 at 21:10
  • @GreenTree, not sure if I understand the question. – SergeyA Dec 15 '16 at 21:18