On Tue, Jul 28, 2009 at 9:53 PM, Roland Steiner <span dir="ltr">&lt;<a href="mailto:rolandsteiner@google.com">rolandsteiner@google.com</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>I definitely like the general idea, but I don&#39;t think a NeverNull template is worth it in the general case, for the following reasons:<br><br>First, I don&#39;t hink you can catch even a meaningful subset of all cases of NULL assignment at compile time. OTOH, writing a template class that wraps a non-null pointer has its use mainly in order to annotate header files, and to &quot;auto-insert&quot; the ASSERT. In cpp files it&#39;s just a cosmetic difference to ASSERTs, and enforcing the use throughout would probably change far too much code, and run into other cumbersome details in addition to the ones I mention below.<br>

<br>OTOH in order to be consequent, one would also need to use NeverNull&lt;X&gt; in return values, e.g.,<br><br><div style="margin-left:40px">NeverNull&lt;foo&gt; bar() const; <br></div><br>Implementing such a function would have to be conscious of the fact that a NeverNull&lt;X&gt; must always be initialized. i.e., you cannot write <br>

<br><div style="margin-left:40px">NeverNull&lt;foo&gt; Baz::bar() const <br>{<br></div><div style="margin-left:80px">NeverNull&lt;foo&gt; returnValue;<br>... compute returnValue ...<br>return returnValue;<br></div><div style="margin-left:40px">

}</div></blockquote><div><br></div><div>Couldn&#39;t you just create a &quot;const foo&amp;&quot; constructor and a &quot;(foo)&quot; operator in your NeverNull template class to get around this?  The constructor would have an ASSERT to check that the value is not NULL, so (in theory) it&#39;d just be a dumb wrapper in release mode.</div>
<div><br></div><div>It&#39;s _possible_ modern day compilers would be smart enough to handle this.  That said, I think the only way to talk inteligently about the performance implications is for someone to try it out on some popular code paths.</div>
</div>