[Insight-developers] Code/Review/itkOptImageToImageMetric strange const issues

Stephen Aylward stephen.aylward at kitware.com
Wed Oct 14 21:29:00 EDT 2009


We need to create a SetSingleMethod member function in the threader
class that takes a const argument.

s

On Wed, Oct 14, 2009 at 7:36 PM, Luis Ibanez <luis.ibanez at kitware.com> wrote:
> Hi Kent,
>
> As you pointed out,
> this section of code in the itkOptImageToImageMetric
> class could benefit from a refactoring.
>
> Const-correctness was enforced in this class, mostly
> as an after-thought, so not all the methods are following
> clean practices.
>
> It would seem that using static_cast<> instead of the
> c-style cast for (void *) should work ok.
>
> I agree with you in the the m_Treader taking a member
> variable in a non-const form should generate a compilation
> error in a const method.
>
> I'm not sure that going around silencing the warning is
> the right thing to do for the long term...
>
> May be we should reconsider some refactoring here
> at some point.
>
>
>
>          Luis
>
> ---------------------------------------------------------------------------------------
> On Wed, Oct 14, 2009 at 11:38 AM, kent williams
> <norman-k-williams at uiowa.edu> wrote:
>> I just checked in a change to Code/Review/itkOptImageToImageMetric.txx that
>> quiets a warning:
>>
>> Insight/Code/Review/itkOptImageToImageMetric.txx:1210: warning: cast from
>> `const itk::ImageToImageMetric<itk::Image<float, 3u>, itk::Image<float, 3u>
>>>::MultiThreaderParameterType*' to `void*' discards qualifiers from pointer
>> target type
>>
>> There are 3 instances of this warning in that file.  The problem is methods
>> like this:
>>
>> template < class TFixedImage, class TMovingImage  >
>> void
>> ImageToImageMetric<TFixedImage,TMovingImage>
>> ::GetValueMultiThreadedPreProcessInitiate( void ) const
>> {
>>  this->SynchronizeTransforms();
>>
>>  m_Threader->SetSingleMethod(GetValueMultiThreadedPreProcess,
>>                              (void *)(&m_ThreaderParameter));
>>  m_Threader->SingleMethodExecute();
>> }
>>
>> I believe the warning comes from the method being tagged as const.  This
>> seems weird to me for several reasons:
>>
>> 1. Even if it doesn't directly modify a class member, it's passing the
>> address of a class member to a second object, which may then modify it.
>>
>> 2. Why are we still using C-style casts?
>>
>> 3. Calling non-const methods on a member variable in a const class method
>> constitute modifying class state -- why are the calls to SetSingleMethod not
>> flagged as violations of the method's const-ness
>>
>> The fix I checked in addresses the warning by nesting a static_cast to const
>> void * inside a const_cast to void *.  That silences the warning, but the
>> whole thing looks fishy to me.
>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the ITK FAQ at: http://www.itk.org/Wiki/ITK_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.itk.org/mailman/listinfo/insight-developers
>>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ITK FAQ at: http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-developers
>



-- 
Stephen R. Aylward, Ph.D.
Director of Medical Imaging Research
Kitware, Inc. - North Carolina Office
http://www.kitware.com
stephen.aylward (Skype)
(919) 969-6990 x300


More information about the Insight-developers mailing list