[Insight-developers] Overloading methods

Bradley Lowekamp blowekamp at mail.nih.gov
Tue Aug 21 15:36:45 EDT 2012


Hello Michael,

Here is the declaration of one of the GetComponent methods:

template< typename T>
    typename EnableIfC<
      IsSame<T, typename NumericTraits<T>::ValueType>::Value,
      T >::Type
  GetComponent(const T pix,
               unsigned int itkNotUsed( idx ) ) const;

This uses the EnableIf idom to answer lets look at a simplification of function:

template< typename T>  T GetComponent(const T pix,  unsigned int idx ) const;

So I just simplified it some. So for function C++ has the ability to implicitly deduce the template parameters from the arguments passed to a function. So if you did the following:

int i;
GetComponent( i, 10 )

C++ can deduce the template parameter T, based on the function argument i being of type int.


There are a couple  C++ tricks to overloading functions that should be considered before the EnableIf idiom should be considered. Using the EnableIf idom is really a last resort when simpler more understandable techniques can not be used.


Brad

On Aug 17, 2012, at 6:17 PM, Michael Stauffer wrote:
> I'm using the new EnableIfC and DisableIfC routines, to try and optimize itkCentralDifferenceImageFunction::Evaluate* methods by specializing for scalar and vector pixel types. I have it working using my own SFINAE method I pulled off the web. But this, requires calling templated subfunctions, which cost about 2% in overhead.
> 
> 
> Looking at itkEnableIf.h and its usage in PatchBasedDenoisingImageFilter, I was hoping to be able to call the specialized Evalute* methods directly.
> 
> 
> The method PatchBasedDenoisingImageFilter::GetComponent is are template-specialized to use one of two versions depending on whether the pixel is scalar or not. The method is templated.
> But in PatchBasedDenoisingImageFilter::ComputeSignedEuclideanDifferenceAndWeightedSquaredNorm(), GetComponent is called without any template parameters. How does this work? I must be missing something.
> 
> 
> 
> 
> template 
> void
> PatchBasedDenoisingImageFilter
> ::ComputeSignedEuclideanDifferenceAndWeightedSquaredNorm(const PixelType& a, const PixelType& b,
> const RealArrayType& weight,
> bool itkNotUsed(useCachedComputations),
> SizeValueType itkNotUsed(cacheIndex),
> EigenValuesCacheType& itkNotUsed(eigenValsCache),
> EigenVectorsCacheType& itkNotUsed(eigenVecsCache),
> RealType& diff, RealArrayType& norm)
> {
> for (unsigned int pc = 0; pc < m_NumPixelComponents; ++pc)
> {
> RealValueType tmpDiff = GetComponent(b, pc) - GetComponent(a, pc);
> RealValueType tmpWeight = weight[pc];
> SetComponent(diff, pc, tmpDiff);
> norm[pc] = tmpWeight * tmpWeight * tmpDiff * tmpDiff;
> }
> }
> 

========================================================
Bradley Lowekamp  
Medical Science and Computing for
Office of High Performance Computing and Communications
National Library of Medicine 
blowekamp at mail.nih.gov



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-developers/attachments/20120821/8ddd17f1/attachment.htm>


More information about the Insight-developers mailing list