Boost logo

Boost :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2004-03-09 17:56:28


----- Mensaje original -----
De: "Powell, Gary" <powellg_at_[hidden]>
Fecha: Martes, Marzo 9, 2004 10:55 pm
Asunto: RE: [boost] Re: Formal Review: Circular Buffer

>
> The VTL library does it IMO correctly, allowing that if there is
> an operation
> T1 F T2, then
>
> V1 F V2 compiles. Otherwise you just force the user to write what
> should be library
> code.
>
> (Obviously swap can't do this unless the Allocators are the same.)
>

I don't quite get you here. I think the two allocator
types cannot be equal: the first allocator allocates T1's,
the second T2's. I might be totally misinterpreting you, of
course.

> template<class T1, class T2, class Alloc>
> void swap( circular_buffer<T1, Alloc> &lhs, circular_buffer<T2,
> Alloc> &rhs)
> {
> // do an element by element swap.
> // and insert for the leftover elements.
> }
>

IMHO doing an elementwise swapping is very ill-behaved.
It would reduce to repeatedly calling something like:

template <typename T1,typename T2>
void swap_different(T1& x,T1& y)
{
  T1 tmp(y);
  x=y;
  y=x;
}

So, for swap to have *some* exception safety the
assignment operators must not throw, which seems too
restrictive (think types with dynamic allocation).
Maybe things can be improved a bit with something along
this line:

template<class T1, class T2, class Alloc1, class Alloc2>
void swap(
  circular_buffer<T1, Alloc1> &lhs,
  circular_buffer<T2, Alloc2> &rhs)
{
  circular_buffer<T1, Alloc1> new_lhs;
  // copy rhs into new_lhs
  circular_buffer<T2, Alloc2> new_rhs;
  // copy lhs into new_rhs

  lhs.swap(new_lhs); // nothrow
  rhs.swap(new_rhs); // nothrow
}

There is a final annoyance, though: in many cases
the code can emit a narrowing cast warning (if assigning
an int to a short, for instance.)

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


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