Topic: What if an exception is thrown while swapping Swappable objects?


Author: unknown@this.is.invalid ("Niels Dekker (no reply address)")
Date: Sun, 24 Sep 2006 18:26:41 GMT
Raw View
When objects of a Swappable class are being swapped, it is allowed for
the swap function to throw an exception, according to the Draft
(N1905).  This was confirmed by both Howard Hinnant and Pete Becker, in
a previous discussion, "Is a Swappable class allowed to throw an
exception?".  Thanks for your replies, sofar!  My next question, though:
how will std functions that impose Swappable requirements onto their
arguments cope with a throwing swap?

For example, suppose the swap function called by swap_ranges might throw
an exception.  Typically this would result in having the two ranges only
partially swapped.  (Which might mean user data getting corrupted!)
Does the caller have a way to tell how much of its ranges has been
swapped successfully, after calling swap_ranges?  Is swap_ranges allowed
to catch the exception thrown by the swap function, and return the value
of the iterator, pointing at where it went wrong?

In other words, is the following implementation of std::swap_ranges
conforming to the Draft?

  template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator2
  swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1,
  ForwardIterator2 first2)
  {
    while (first1 != last1)
    {
      try
      {
        swap(*first1, *first2);
      }
      catch(...)
      {
        return first2;
      }
      ++first1;
      ++first2;
    }
    return first2;
  }


Kind regards,

  Niels Dekker

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: dave@boost-consulting.com (David Abrahams)
Date: Mon, 25 Sep 2006 16:42:36 GMT
Raw View
unknown@this.is.invalid ("Niels Dekker (no reply address)") writes:

> When objects of a Swappable class are being swapped, it is allowed for
> the swap function to throw an exception, according to the Draft
> (N1905).  This was confirmed by both Howard Hinnant and Pete Becker, in
> a previous discussion, "Is a Swappable class allowed to throw an
> exception?".  Thanks for your replies, sofar!  My next question, though:
> how will std functions that impose Swappable requirements onto their
> arguments cope with a throwing swap?

The same way they deal today with element copies throwing during sort,
swap_ranges, etc.

> For example, suppose the swap function called by swap_ranges might throw
> an exception.  Typically this would result in having the two ranges only
> partially swapped.  (Which might mean user data getting corrupted!)

The standard library only maintains (and can only maintain) the
invariants that are visible to it.  Users are responsible for
maintaining their own invariants.  If being "uncorrupted" according to
some higher-level user invariant depends on the ranges either being
fully swapped or fully unswapped, it's the user's job to ensure that.

> Does the caller have a way to tell how much of its ranges has been
> swapped successfully, after calling swap_ranges?

No.  And what would the caller do with that information anyway?  Any
attempt to unswap the swapped parts could throw, too.

> Is swap_ranges allowed to catch the exception thrown by the swap
> function, and return the value of the iterator, pointing at where it
> went wrong?

No.  I don't think it's explicitly stated anywhere, but standard
library algorithms are intended to be exception-neutral.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]