Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think <strong>it is an optimisation</strong>! A little known and rarely (you could say "never") used feature of <code>typeid</code> is that a null dereference of the argument of <code>typeid</code> throws an exception instead of the usual UB.</p> <blockquote> <p>What? Are you serious? Are you drunk?</p> </blockquote> <p>Indeed. Yes. No.</p> <pre> int *p = 0; *p; // UB typeid (*p); // throws </pre> <p>Yes, this is ugly, even by the C++ standard of language ugliness.</p> <p>OTOH, this does not work anywhere <strong>inside</strong> the argument of <code>typeid</code>, so adding any clutter will cancel this "feature":</p> <pre> int *p = 0; typeid(1 ? *p : *p); // UB typeid(identity(*p)); // UB </pre> <p>For the record: I am not claiming in this message that automatic checking by the compiler that a pointer is not null before doing a dereference is necessarily a crazy thing. I am only saying that doing this check when the dereference is the immediate argument of <code>typeid</code>, <strong>and not elsewhere</strong>, is totally crazy. (Maybe is was a prank inserted in some draft, and never removed.)</p> <p>For the record: I am not claiming in the previous "For the record" that it makes sense for the compiler to insert automatic checks that a pointer is not null, and to to throw an exception (as in Java) when a null is dereferenced: in general, throwing an exception on a null dereference is absurd. This is a programming error so an exception will not help. An assertion failure is called for.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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