<HTML>
<HEAD>
<TITLE>Re: [vtk-developers] vtkSmartPointer and gcc -Wunused-variable</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>GCC is not warning about an unused variable because your declaration includes lots of semantic sugar to hide actual code that gets executed. &nbsp;Specifically, the custom constructor and destructor of the smart pointer are called, and the compiler has no way to know that the constructor and destructor does not have important global effects. &nbsp;In fact, there are times where the smart pointer may be used almost exactly like this where it has important side effects that you would not want the compiler to complain about. &nbsp;For example, consider this small change to your code:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'>vtkPolyData *pd = vtkPolyData::New();<BR>
vtkSmartPointer&lt;vtkPolyData&gt; polydata = pd;<BR>
pd-&gt;Delete();<BR>
</SPAN></FONT></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
As far as the compiler is concerned, not much has changed; a smart pointer is constructed with a regular pointer. &nbsp;But this time there is a very important side effect in that the operations in the constructor are preventing the object from being deleted until the smart pointer leaves scope and its destructor is called. &nbsp;Because there is no reliable way for the compiler to determine the usefulness of the constructor and destructor, it always assumes that they do something important.<BR>
<BR>
-Ken<BR>
<BR>
<BR>
On 11/24/10 2:50 PM, &quot;Sean McBride&quot; &lt;<a href="sean@rogue-research.com">sean@rogue-research.com</a>&gt; wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>Hi all,<BR>
<BR>
Consider this simple code:<BR>
<BR>
-------<BR>
#include &quot;vtkSmartPointer.h&quot;<BR>
#include &quot;vtkPolyData.h&quot;<BR>
<BR>
int main (int argc, char *argv[])<BR>
{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)argc; (void)argv;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int a = 0;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vtkSmartPointer&lt;vtkPolyData&gt; polydata = 0;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<BR>
}<BR>
-------<BR>
<BR>
Then:<BR>
<BR>
$ gcc -Wunused-variable -I/vtk/include/vtk-5.7 test.cp<BR>
<BR>
test.cp: In function &#8216;int main(int, char**)&#8217;:<BR>
test.cp:8: warning: unused variable &#8216;a&#8217;<BR>
<BR>
Notice that gcc does *not* warn that &quot;polydata&quot; is unused, but does for<BR>
&quot;a&quot;. &nbsp;I'm curious why that is, and if anyone knows of a way to get gcc<BR>
to warn? &nbsp;(clang also does not warn.)<BR>
<BR>
Cheers,<BR>
<BR>
--<BR>
____________________________________________________________<BR>
Sean McBride, B. Eng &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="sean@rogue-research.com">sean@rogue-research.com</a><BR>
Rogue Research &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;www.rogue-research.com<BR>
Mac Software Developer &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Montr&eacute;al, Qu&eacute;bec, Canada<BR>
_______________________________________________<BR>
Powered by www.kitware.com<BR>
<BR>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html">http://www.kitware.com/opensource/opensource.html</a><BR>
<BR>
Follow this link to subscribe/unsubscribe:<BR>
<a href="http://www.vtk.org/mailman/listinfo/vtk-developers">http://www.vtk.org/mailman/listinfo/vtk-developers</a><BR>
<BR>
<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'><BR>
&nbsp;&nbsp;&nbsp;**** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kenneth Moreland<BR>
&nbsp;&nbsp;&nbsp;&nbsp;*** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sandia National Laboratories<BR>
*********** &nbsp;<BR>
*** *** *** &nbsp;email: <a href="kmorel@sandia.gov">kmorel@sandia.gov</a><BR>
** &nbsp;*** &nbsp;** &nbsp;phone: (505) 844-8919<BR>
&nbsp;&nbsp;&nbsp;&nbsp;*** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;web: &nbsp;&nbsp;<a href="http://www.cs.unm.edu/~kmorel">http://www.cs.unm.edu/~kmorel</a><BR>
</SPAN></FONT></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT>
</BODY>
</HTML>