
From: David Abrahams <dave@boost-consulting.com>
Rob Stewart <stewart@sig.com> writes:
swap() is typically as fast as or faster than copy assignment,
Not for many types that one might like to reset, such as pointers, shared_ptr<T>, and integral types.
I was trying to make the case that a one size fits all reset() is not likely to work. However, in my zeal, I overstated the case for swap(), particularly as it applies to simple types, and you were right to call me on it. swap() on those simple types, of course, involves a temporary and two assignments, which I was neglecting and, yes, those are the most likely candidates for reset(), at least for the purpose being discussed. Note, however, that a namespace scope reset() can be construed as meaning different things to different types. For example, it could be specialized to provide the fastest means to reset any object to its initial state. Thus, it might do std::vector<T>().swap(v) or something uglier for other types. Thus, reset() should be designed and documented to be customizable for a reasonable meaning of "reset" for any type. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;