I just wanted to highlight a problem I've just encountered :
vil_image_view<vxl_int32> im1 (5,5,5);
vil_image_view<vxl_byte> = vil_plane(im1, 0); // Silent failure.
In this case, I can't see any reason for not aborting in the copy operator,
because vil_plane returns something of type vil_image_view<vxl_int32>, which
we could even know at compile time to be invalid.
However, in the case of vil_load, which returns an image_view_sptr, we cannot
make sure that the assignment will succeed before actually calling the
function, so I think aborting is a bit too strong, and using a temporary
image_view_sptr variable seems complicated. And I guess exceptions are not
portable enough for vxl. There is the same problem with iostreams, so why not
behaving the same way ?
vil_image_view<vxl_byte> im = vil_load("foo");
if (!im) abort(); // or assert(im);
So what I suggest is defining an additional operator= like this (it supposes
that we can use templates member function though) :
const vil_image_view<T>& operator=(const vil_image_view<U> & rhs)
abort(); // or even static_assertion(false);
This way obvious static programming mistakes would be catched, and dynamic
stuff would still have to be checked, but this seems to be an accepted idiom
in the C++ world anyway ...
On Friday 17 June 2005 11:57, Tim Cootes wrote:
> Hi Amitha,
> I agree that failing silently is not terribly satisfactory, as it is
> hard to track down.
> Strictly, of course, one should write:
> vil_image_view<vxl_byte> im=vil_load(filepath);
> if (im.size()==0)
> // Uh-oh!
> vcl_cerr<<"I told you not to do that."<<vcl_endl;
> However, we don't all remember to do this.
> If one were to abort() on a failure to do a cheap conversion, one would
> have to write
> vil_image_view_base_sptr im_ptr=vil_load(filepath);
> if (!im_ptr)
> // Failed to load anything...
> if (im_ptr->is_a()!="vil_image_view<vxl_byte>")
> // Wrong type! Convert or die
> This is a bit longer, but more explicit.
> On the grounds that silent failure is generally a bad thing, I wouldn't
> object to the copy operator aborting if it can't do it easily.
> However, Ian has a deeper knowledge of vil than I, and may have some
> Amitha Perera wrote:
> >I'd like to change the vil_image_view::operator= API to abort() if it
> >cannot do a cheap assignment. I've already been bitten multiple times
> >by the silent failure where the source and target types don't
> >match. One particular example is
> > vil_image_view<vxl_byte> im = vil_load( "16-bit image" );
> >will simply return a null image, and can take a lot of tracking down
> >to figure out that there is a type mismatch.
> >Does someone recall the original justification for silent failure? I
> >suggest that if "cheap conversion compatibility" needs to be checked,
> >then we create a new method "is_compatible", so that operator= will
> >succeed iff is_compatible returns true.
> >My next email will contain suggestion to make this error a compile
> >time error when possible.
> >SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
> >from IBM. Find simple to follow Roadmaps, straightforward articles,
> >informative Webcasts and more! Get everything you need to get up to
> >speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
> >Vxl-maintainers mailing list