On Tue, Jul 28, 2009 at 9:53 PM, Roland Steiner <span dir="ltr"><<a href="mailto:rolandsteiner@google.com">rolandsteiner@google.com</a>></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't think a NeverNull template is worth it in the general case, for the following reasons:<br><br>First, I don'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 "auto-insert" the ASSERT. In cpp files it'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<X> in return values, e.g.,<br><br><div style="margin-left:40px">NeverNull<foo> bar() const; <br></div><br>Implementing such a function would have to be conscious of the fact that a NeverNull<X> must always be initialized. i.e., you cannot write <br>
<br><div style="margin-left:40px">NeverNull<foo> Baz::bar() const <br>{<br></div><div style="margin-left:80px">NeverNull<foo> returnValue;<br>... compute returnValue ...<br>return returnValue;<br></div><div style="margin-left:40px">
}</div></blockquote><div><br></div><div>Couldn't you just create a "const foo&" constructor and a "(foo)" 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'd just be a dumb wrapper in release mode.</div>
<div><br></div><div>It'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>