title: Why is dereferencing of nullptr while using a static method not undefined behaviour in C++?
body: <p>I was reading <a href="https://dev.to/promwad_team/null-pointers-in-c-what-you-can-and-can-t-do-25ic" rel="noreferrer">a post on some <code>nullptr</code> peculiarities in C++</a>, and a particular example caused some confusion in my understanding.</p>
  <p>Consider (simplified example from the aforementioned post):</p>
  <pre><code>struct A {   
      void non_static_mem_fn() {}  
      static void static_mem_fn() {}  
  };
  
  
  A* p{nullptr};
  
  /*1*/ *p;
  /*6*/ p-&gt;non_static_mem_fn();
  /*7*/ p-&gt;static_mem_fn();
  </code></pre>
  <p>According to the authors, expression <code>/*1*/</code> that dereferences the <code>nullptr</code> does not cause undefined behaviour by itself. Same with expression <code>/*7*/</code> that uses the <code>nullptr</code>-object to call a static function.</p>
  <p>The justification is based on <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#315" rel="noreferrer">issue 315 in C++ Standard Core Language Closed Issues, Revision 100</a> that has</p>
  <blockquote>
  <p>...<code>*p</code> is not an error when <code>p</code> is null unless the lvalue is converted to an rvalue (7.1 [conv.lval]), which it isn't here.</p>
  </blockquote>
  <p>thus making a distinction between <code>/*6*/</code> and <code>/*7*/</code>.</p>
  <p>So, the actual dereferencing of the <code>nullptr</code> <strong>is not undefined behaviour</strong> (<a href="https://stackoverflow.com/a/6716109/2452869">answer on SO</a>, <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232" rel="noreferrer">discussion under issue 232 of C++ Standard</a>, ...). Thus, the validity of <code>/*1*/</code> is understandable under this assumption.</p>
  <p>However, how is <code>/*7*/</code> guaranteed to not cause UB? As per the cited quote, there is no conversion of lvalue to rvalue in <code>p-&gt;static_mem_fn();</code>. But the same is true for <code>/*6*/</code> <code>p-&gt;non_static_mem_fn();</code>, and I think my guess is confirmed by the quote from the same issue 315 regarding:</p>
  <blockquote>
  <p><code>/*6*/</code> is explicitly noted as undefined in 12.2.2
  [class.mfct.non-static], even though one could argue that since <code>non_static_mem_fn();</code> is
  empty, there is no lvalue-&gt;rvalue conversion.</p>
  </blockquote>
  <p>(in the quote, I changed &quot;which&quot; and <code>f()</code> to get the connection to the notation used in this question).</p>
  <hr />
  <p>So, why is such a distinction made for <code>p-&gt;static_mem_fn();</code> and <code>p-&gt;non_static_mem_fn();</code> regarding the causality of UB? Is there an intended use of calling static functions from pointers that could potentially be <code>nullptr</code>?</p>
  <hr />
  <p>Appendix:</p>
  <ul>
  <li><a href="https://stackoverflow.com/q/6793262/2452869">this question asks about why dereferencing a <code>nullptr</code> is undefined behaviour</a>. While I agree that in most cases it is a bad idea, I do not believe the statement is absolutely correct as per the links and quotes here.</li>
  <li>similar discussion <a href="https://stackoverflow.com/q/39929099/2452869">in this Q/A</a> with some links to issue 232.</li>
  <li>I was not able to find a question devoted to static methods and the <code>nullptr</code> dereferencing issue. Maybe I missed some obvious answer.</li>
  </ul>