[Insight-developers] Potential SmartPointer improvement

Hauke Heibel hauke.heibel at googlemail.com
Fri Jul 30 06:41:49 EDT 2010


Hi,

I am wondering what you think about the following modification of the
SmartPointer's copy constructor.

  template<class OtherObjectType>
  SmartPointer (const SmartPointer<OtherObjectType> &p):
  m_Pointer(p.GetPointer())
  { this->Register(); }

The function can be even improved on C++0x systems by enabling it only
when ObjectType* and OtherObjectType* are convertible to each other
(see std::is_convertible). Though I am not sure, whether you already
have a define letting you know about the availability of C++0x type
traits.

This will improve the capability of writing const correct code.
Currently, it is not possible to write the following code:

typedef itk::Image<unsigned char, 3> ImageType;

typedef ImageType::Pointer SharedImage3d;
typedef ImageType::ConstPointer ConstSharedImage3d;

void test(ConstSharedImage3d image)
{
  // process image
}

void main()
{
  // some code
  SharedImage3d image;
  test(image); // fails
}

However, currently this call is working

  test(ConstSharedImage3d(image));

because the SharedImage3d converts itself to ObjectType* and then, the
corresponding constructor from ConstSharedImage3d is called.

- Hauke


More information about the Insight-developers mailing list