Boost logo

Boost :

From: Pavel Chikulaev (pavel.chikulaev_at_[hidden])
Date: 2005-04-13 09:13:58


"Philippe Mori" <philippe_mori_at_[hidden]> wrote:
> My opinion is that we should only have one swap function per object
> type named swap. And we should always make it such that it won't
> throws.

What if class only has Swap function? What to do then?
Or swap can throw? (boost::array for example)

> I think that the best thing would be that std::swap would only compile
> for a given type T if the resulting function cannot throws (because
> copy constructor and assigment would not throws) and thus having a
> type trait would not be as much usefull.

It's not possible since std::swap implementated for not specialized type
as follows:
T t(a);
a = b;
b = t;
So, we can be sure in anything.
Will it throw or not?
Is it optimal?
We just can't answer these questions.

> OTOH, if we add type trait to indicate if swap is possible or if it will
> throw,
> then the defaut implementation should be able to deduce it from the traits
> of the copy constructor and assignment operator. In particular swap of
> POD types does not throws.

Yeah, some assumptions can be made, but not only for POD types and some
other trivial cases.
So we need to explicitly register type.

> Also the compiler should be able to detect throw () specification and
> provide the type trait itself.

Too many people don't use throw specifications, but their swap still not throws.

Thanks for comments, Philippe.

--
Pavel Chikulaev 

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