[Insight-developers] reinterpret_cast : asking for help

Gaëtan Lehmann gaetan.lehmann at jouy.inra.fr
Wed Feb 18 05:21:52 EST 2009


Le 18 févr. 09 à 04:51, Luis Ibanez a écrit :

>
> Hi Gaetan,
>
> Could you help us understand the use of "reinterpret_cast" in the
> itkKernelImageFilter.txx file ?
>
> Currently it generates a warning, and... further reading about
> the reinterpret_cast lead us to think that it shouldn't be used
> in this case (e.g. to convert high level classes to others).
>
> The preceding test of typeid() comparisons may also, not be
> performing the expected task. Please note that in the Events
> we end up using dynamic_cast in order to check whether one
> class was actually derived from another.


Hi Luis,

The reinterpret_cast was used to avoid a warning - it seems to not be  
as successful has expected...

KernelImageFilter has to work with any kind of kernel -  
itk::Neighborhood, BinaryBallStructuringElement, ..., and  
FlatStructuringElement.
FlatStructuringElement is a bit special, because it can't be simply  
filled as the other structuring element to produce the best  
performances in some circonstances: for a box structuring element, we  
have to call Box() to produce a decomposable kernel suitable for the  
van Herk / Gil Werman / anchor filters. The method SetRadius(), where  
the reinterpret_cast is, produce a box structuring element, and so it  
calls FlatStructuringElement::Box() to get the best performance when  
possible.

The problem is to make that build without warning with any type of  
structuring element used to instantiate the KernelImageFilter.
An example: the user instantiate KernelImageFilter with  
BinaryBallStructuringElement< float, 3 > as template parameter. In  
that case, the user defined structuring element has no super class in  
common with FlatStructuringElement< 3 > required to call Box(), and  
thus it becomes impossible to make a static_cast build, and impossible  
to make a dynamic cast build without warning like this one:

/Users/glehmann/src/Insight/Code/Review/itkKernelImageFilter.txx:50:  
warning: dynamic_cast of ‘itk::FlatStructuringElement<2u> flatKernel’  
to ‘class itk::BinaryBallStructuringElement<float, 2u,  
itk::NeighborhoodAllocator<float> >*’ can never succeed

The reinterpret_cast let the code build without warning (here), and  
the type check is there to be sure that something nasty is done.

Explicit specialization of a method is not allowed in a non  
specialized class, and I guess partial specialization is still not  
usable in ITK to be able to build with msvc6. There is still the  
overloaded templated/not templated method workaround, but I'm not sure  
how it works on all the supported compilers - see attached patch.
The last option would be to use a specialized helper function.

Those options seemed to have less chance to build everywhere, at the  
time I've implemented that, so I preferred the reinterpret_cast way.  
But as you get warnings, it may be better to try the other ones!

Regards,

Gaëtan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: kernel.patch
Type: application/octet-stream
Size: 2925 bytes
Desc: not available
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20090218/1dc08256/attachment.obj>
-------------- next part --------------



-- 
Gaëtan Lehmann
Biologie du Développement et de la Reproduction
INRA de Jouy-en-Josas (France)
tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
http://voxel.jouy.inra.fr  http://www.mandriva.org
http://www.itk.org  http://www.clavier-dvorak.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: Ceci est une signature ?lectronique PGP
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20090218/1dc08256/attachment.pgp>


More information about the Insight-developers mailing list