[Insight-developers] Overloading methods
Williams, Norman K
norman-k-williams at uiowa.edu
Thu Aug 23 15:09:22 EDT 2012
Though it isn't the case here, another problem with compiler template
deductions is that it won't deduce the template for template<typename T>
if the argument isn't a simple use of T. To illustrate:
this will work:
template <typename TImage>
void DoIt(const TImage *imagePtr)
{
}
typedef itk::Image<char,3> ImageType;
ImageType::Pointer iPtr;
Doit(iPtr->GetPointer()); // const ImageType *imagePtr
but this will not:
template <typename TPixel>
void DoIt(const typename itk::Image<TPixel,3> *iPtr)
{
}
typedef itk::Image<char,3> ImageType;
ImageType::Pointer iPtr;
DoIt(iPtr->GetPointer()); // asks compiler to deduce
// TPixel from ImageType::Pointer, by
way of implicit
// conversion from ImageType::Pointer to
ImageType *
// Compiler not that smart.
--
Kent Williams norman-k-williams at uiowa.edu
On 8/23/12 7:21 AM, "Bradley Lowekamp" <blowekamp at mail.nih.gov> wrote:
>
>
>
>Hello,
>
>
>The reason you methods is not working is because C++ can't deduce the
>template parameters from the argument types passed to the function. That
>is to say since T is only used for the return type, and not as an
>argument to the function, so you should explicitly
> specify it as a template parameters. Something like this should work:
>
>
>r = this->EnableIfTest<OutputType>( 3 );
>
>
>Brad
>
>
>
>On Aug 22, 2012, at 4:08 PM, M Stauffer -V- wrote:
>
>
>Great, thanks Brad.
>
>I can reproduce what you explain below, but it's not working when I rely
>on only the return parameter type for the template parameter deduction.
>That is, the following
> won't compile
>
>template< typename T >
>typename EnableIfC< IsSame< T, ScalarDerivativeType >::Value, T >::Type
>EnableIfTest(int val)
>{
> std::cout << "Output == ScalarDerivativeType. val " << val << "\n" <<
>std::endl;
> T o;
> o.Fill(1);
> return o;
>}
>
>(and similarly with DisableIfC...)
>
>In implementation:
>
> OutputType r;
> r = this->EnableIfTest(3);
>
>I think I'll skip this for now because I have a working implementation
>and the extra overhead it has, because it requires a nested function
>call, is only a few %.
>
>I'll put up a patch soon and hopefully you could take a look at it, Brad.
>I'll let you know.
>
>-M
>
>
>
>
>
>
>________________________________________
>From: Bradley Lowekamp [mailto:blowekamp at mail.nih.gov]
>
>Sent: Tuesday, August 21, 2012 3:37 PM
>To: Michael Stauffer
>Cc: Insight-developers at itk.org
>Subject: Re: [Insight-developers] Overloading methods
>
>
>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::ComputeSignedEuclideanDifferenceAndWeighte
>dSquaredNorm(), 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
>
>
>
>
>
>
>
>
>
>
>
>
>
>========================================================
>Bradley Lowekamp
>Medical Science and Computing for
>Office of High Performance Computing and Communications
>National Library of Medicine
>blowekamp at mail.nih.gov
>
>
>
>
>
>
>
________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged. If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited. Please reply to the sender that you have received the message in error, then delete it. Thank you.
________________________________
More information about the Insight-developers
mailing list