<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.  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.  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.  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<vtkPolyData> polydata = pd;<BR>
pd->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.  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.  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, "Sean McBride" <<a href="sean@rogue-research.com">sean@rogue-research.com</a>> 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 "vtkSmartPointer.h"<BR>
#include "vtkPolyData.h"<BR>
<BR>
int main (int argc, char *argv[])<BR>
{<BR>
        (void)argc; (void)argv;<BR>
       <BR>
        int a = 0;<BR>
        vtkSmartPointer<vtkPolyData> polydata = 0;<BR>
       <BR>
        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 ‘int main(int, char**)’:<BR>
test.cp:8: warning: unused variable ‘a’<BR>
<BR>
Notice that gcc does *not* warn that "polydata" is unused, but does for<BR>
"a".  I'm curious why that is, and if anyone knows of a way to get gcc<BR>
to warn?  (clang also does not warn.)<BR>
<BR>
Cheers,<BR>
<BR>
--<BR>
____________________________________________________________<BR>
Sean McBride, B. Eng                 <a href="sean@rogue-research.com">sean@rogue-research.com</a><BR>
Rogue Research                        www.rogue-research.com<BR>
Mac Software Developer              Montréal, Qué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>
   ****      Kenneth Moreland<BR>
    ***      Sandia National Laboratories<BR>
***********  <BR>
*** *** ***  email: <a href="kmorel@sandia.gov">kmorel@sandia.gov</a><BR>
**  ***  **  phone: (505) 844-8919<BR>
    ***      web:   <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>