Boost logo

Boost :

From: Eric Friedman (ebf_at_[hidden])
Date: 2003-10-07 22:32:40


Eugene,

E. Gladyshev wrote:

> --- Eric Friedman <ebf_at_[hidden]> wrote:
> [...]
>>* What exactly do you mean by "variant protection"? The reason heap
>>backup can be turned off in certain cases is because it is *not needed*
>>in those cases. The guarantees made by variant are always the same.
>
>
> I guess I am missing something here.
>
> I thought that the heap backup is supposed to protect variant
> from possible exceptions during assignments.

The heap backup is used so that -- even in the event of an exception
during assignment -- variant can continue to hold one of its bounded
types. There is no other reason it is used. So if by "protection" you
mean "exception safety," then you've understood. Otherwise, you are
missing the point.

Perhaps outlining the variant assignment operation might help. Given lhs
and rhs both of type variant<T1,...,TN> (that is, ignoring any
conversions), the basic idea is this:

   if (typeof(lhs.content) is typeof(rhs.content)) {
     lhs.content = rhs.content;
   }
   else if (typeof(rhs.content) has nothrow copy) {
     destroy lhs.content;
     copy rhs.content into lhs.storage;
   }
   else if (T1 has nothrow default-construct) {
     destroy lhs.content;
     try {
       copy rhs.content into lhs.storage;
     }
     catch (...) {
       default-construct T1 in lhs.storage;
       rethrow exception;
     }
   }
   else {
     backup_ptr = (copy lhs.content onto heap);
     destroy lhs.content;

     try {
       copy rhs.content into lhs.storage;
     }
     catch (...) {
       copy backup_ptr into lhs.storage;
       indicate using backup;
       rethrow exception;
     }

     delete backup_ptr;
   }

HTH,
Eric


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk