From: Paul Moore (gustav_at_[hidden])
Date: 1999-12-18 18:16:43
From: Alan Griffiths [mailto:alan_at_[hidden]]
> Let me relate a problem from "real life"...
> Cast: two "public" programmers Janet and John, and a library implementor
> Janet writes a class using std::vector<>, and provides a swap() method
> and a free swap() function (as someone on this thread has already
> described). She'd been paying attention during my seminar on exception
> safety. The swap function looked something like:
> C& C::swap(C& that)
> using std::swap;
> swap(a, that.a);
> swap(b, that.b);
> swap(c, that.c);
> swap(d, that.d);
> . . .
> Some months later John comes along and decides that a container (call it
> arg::vector) that I wrote would be better than std::vector. He changes
> the class definition, re-runs the unit test harness and commits the
> The problem is, of course, that the resulting code /1/ no longer meets
> the strong exception safety guarantee and /2/ is a lot slower. (I had
> provided an efficient, non-throwing swap<> in namespace arg.)
At the risk of exposing my ignorance, isn't that what Koenig lookup does for
you? I assume that you are saying that a etc were std::vector and were changed
to arg::vector. Doesn't Koenig lookup say that swap() is searched for in the
namespace containing a's type - ie std:: or arg:: as appropriate?
If that ISN'T what the lookup rules imply, then I agree with you. But I'm
working on the basis that Janet's code above, even without the using std::swap,
works whatever namespace a is in.
Regardless of the rights and wrongs, I'd have to say that this is all extremely
subtle, and *not* the sort of stuff people could be expected to get right
without better guidelines.
Thanks for the comments,
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk