[Insight-developers] finding/using type_traits facility from C++0x?

kent williams norman-k-williams at uiowa.edu
Wed Oct 27 12:23:08 EDT 2010


I think Gaetan¹s idea works in his context but not in this one:
He uses the template function as a fallback for the compiler to choose if
the non-template functions don¹t match.  The CannySegmentationLevelSet case
is ambiguous in the way Gaetan¹s case was not.

Instead of:
> 
> #include <iostream>
> class A {};
> class B {};
> 
> class C
> {
> public:
> template <class T> void callMe(T &arg)
> {
>   std::cout << "Template" << std::endl;
> }
> void callMe(A &arg)
> {
>   std::cout << "non-Template" << std::endl;
> }
> };
> 
> int main(int argc,char **argv)
> {
> A a; B b;
> C c;
> c.callMe(a); // chooses non-template, best match
> c.callMe(b); // chooses template, fallback
> }
> 

I have this situation:

> #include <iostream>
> 
> class A {};
> class B {};
> 
> template <class Ta, class Tb>
> class C
> {
> public:
>   // how to tell the compiler to call one if Ta = Tb,
>   // and the other if Ta != Tb,
>   // if the argument is always going to be Tb?
>   template <class _Tb> void callMe(_Tb &arg)
>   {
>     std::cout << "Template" << std::endl;
>   }
>   void callMe(Tb &arg)
>   {
>     std::cout << "Non-Template" << std::endl;
>   }
> };
> 
> int main(int argc, char **argv)
> {
>   C<A,A> CSame;
>   C<A,B> CDiff;
>   A a; B b;
>   CSame.callMe(a); // non-template called
>   CDiff.callMe(b);   //  non-template called
> }
> 

Within the class context, I don¹t see how you implement the overload to
distinguish between the Ta==Tb and the Ta!=Tb cases.  If there is a way I¹m
missing I¹m open to doing things that  way.

On 10/27/10 10:36 AM, "Bradley Lowekamp" <blowekamp at mail.nih.gov> wrote:

> 
> I like Gaetan's approach the best. It should also be the simplest to implement
> as it just uses member function overloading, and doesn't needed additional
> classes or infrastructure. The compiler should choose the non-templated member
> function before the templated one.
> 
> Brad
> 
> 
> On Oct 27, 2010, at 11:27 AM, kent williams wrote:
> 
>> Having investigated this, and looked at Tom V¹s version, I have this
>> implementation question:
>> 
>> To use the standard type_traits template metaprogramming facility, you have
>> to include type_traits.
>> 
>> Apparently in the latest Visual C++, type_traits is a standard include, and
>> the is_same class test is in the std:: namespace.
>> If you¹re using GCC 4.x.x, the header is in the tr1 subdirectory and the
>> is_same test is in the std::tr1:: namespace.
>> Any other compiler, who knows?
>> 
>> I added a test for type_traits in the top-level CMakeLists.txt and on OS X it
>> isn¹t found, because the tr1 include directory isn¹t in the system include
>> path.
>> 
>> Should I assume C++0x is too new to mess with and use my own version of the
>> is_same type test, or should I try and come up with a comprehensive way to
>> find type_traits or punt and define my own is_same test?
>> 
>> 
>> 
>> On 10/26/10 12:16 PM, "Bradley Lowekamp" <blowekamp at mail.nih.gov
>> <x-msg://260/blowekamp@mail.nih.gov> > wrote:
>> 
>>> Hello Kent,
>>> 
>>> This is a lot easier now that we can use partial template specialization.
>>> This reminds me of the common IsSameType construct (which you tried
>>> something very similar with a function):
>>> 
>>>  /// generic programming to test if T is the same type as U
>>>     template <typename T, typename U>
>>>     struct IsSameType {
>>>       /// true if T is the same type as U
>>>       static const bool result = false;
>>>     };
>>>     
>>>     
>>>     /// generic programming to test if T is the same type as U
>>>     template <typename T>
>>>     struct IsSameType<T,T> {
>>>       /// true if T is the same type as U
>>>       static const bool result = true;
>>>     };
>>> 
>>> 
>>> One way to address this is to define a class in the member function or as a
>>> member of class. I believe you can do the needed partial specialization in
>>> both of these scopes. Then just add a member function to the above construct
>>> that is specific to that specialization.
>>> 
>>> This is what I would try... not 100% it'll work though.
>>> 
>>> Good luck,
>>> Brad
>>> 
>>> On Oct 26, 2010, at 12:36 PM, kent williams wrote:
>>> 
>>>> I'm trying to fix this bug:
>>>> http://public.kitware.com/Bug/view.php?id=3610
>>>> 
>>>> the problem is this code:
>>>> 
>>>>   typename TFeatureImageType::Pointer tempFeature;
>>>>   if ( typeid(TImageType) == typeid(TFeatureImageType))
>>>>     {
>>>>     m_Canny->SetInput(tempFeature);
>>>>     }
>>>>   else
>>>>     {
>>>>     m_Caster->SetInput(tempFeature);
>>>>     m_Canny->SetInput(m_Caster->GetOutput());
>>>>     }
>>>> 
>>>> This code breaks if typeid(TImageType) != typeid(TFeatureImageType), since
>>>> the compiler still compiles the top branch of the if statement, and the
>>>> m_Canny->SetInput() call will be in error because of incorrect parameter
>>>> type.
>>>> 
>>>> I thought I could fix this with specialization, like this:
>>>>> // IN CLASS DEFINITION
>>>>> template <class _TImageType, class _TFeatureImageType>
>>>>> void AssignCannyInput(typename _TFeatureImageType::Pointer &input)
>>>>> {
>>>>>   m_Caster->SetInput(input);
>>>>>   m_Canny->SetInput(m_Caster->GetOutput());
>>>>> }
>>>>> template <>
>>>>> void AssignCannyInput<TImageType,TImageType>(typename TImageType::Pointer
>>>>> &input)
>>>>> {
>>>>>   m_Canny->SetInput(input);
>>>>> }
>>>> 
>>>> But the compiler objects to the specialization inside the class definition,
>>>> and moving the specialization out of the class like this:
>>>> 
>>>>> // Removed from class definition, moved to TXX file
>>>>> template< class TImageType, class TFeatureImageType >
>>>>> template <>
>>>>> void
>>>>> CannySegmentationLevelSetFunction< TImageType, TFeatureImageType >
>>>>> ::AssignCannyInput<TImageType,TImageType>
>>>>> (typename TImageType::Pointer &input)
>>>>> {
>>>>>   m_Canny->SetInput(input);
>>>>> }
>>>> 
>>>> Is illegal as well.
>>>> 
>>>> Does anyone know of a good Template MetaProgramming solution to this issue?
>>>> <ATT00001..txt>
>>> 
>>> ========================================================
>>> 
>>> Bradley Lowekamp
>>> 
>>> Lockheed Martin Contractor for
>>> 
>>> Office of High Performance Computing and Communications
>>> 
>>> National Library of Medicine
>>> 
>>> blowekamp at mail.nih.gov <x-msg://260/blowekamp@mail.nih.gov>
>>> 
>>> 
>>> 
> 
> ========================================================
> 
> Bradley Lowekamp 
> 
> Lockheed Martin Contractor for
> 
> Office of High Performance Computing and Communications
> 
> National Library of Medicine
> 
> blowekamp at mail.nih.gov
> 
> 
> 

------ End of Forwarded Message

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20101027/f5741da7/attachment.htm>


More information about the Insight-developers mailing list