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

Tom Vercauteren tom.vercauteren at m4x.org
Wed Oct 27 11:54:03 EDT 2010


Hi Kent,

I agree that Gaetan's approach will be easier to set up in ITK.

That being said, I'd like to hear what the plan is for ITK w.r.t.
c++0x / TR1. The only place I have seen it mention is on the wishlist:
http://www.itk.org/Wiki/ITK_Release_4/Wish_List#Backward_compatibility_and_cleanup

Back to your question, if you need to backport some of this "standard"
code into ITK, one option could be to do something similar to
itkIntTypes.h:
http://github.com/Kitware/ITK/blob/master/Code/Common/itkIntTypes.h

This should be doable with something along these lines:

#if defined( ITK_HAVE_STD_TYPE_TRAITS )
#include <type_traits>
#endif

namespace itk
{
#if defined( ITK_HAVE_STD_TYPE_TRAITS )
using ::std::is_same;
#else
// put your own implementation here
#endif
}

My feeling is that sadly c++0x might be too new to use in ITK mostly
because of mac. It indeed looks like the to-be-release XCode 4, will
not ship with c++Ox support. Since I am not a mac expert, I'd love to
hear someone contradict me on this point.

My two cents,
Tom

On Wed, Oct 27, 2010 at 17:36, 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> 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
>
>
>
>
> ========================================================
>
> Bradley Lowekamp
>
> Lockheed Martin Contractor for
>
> Office of High Performance Computing and Communications
>
> National Library of Medicine
>
> blowekamp at mail.nih.gov
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://kitware.com/products/protraining.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
>
>


More information about the Insight-developers mailing list