
Hi all, do we have a non-member reset() somewhere? Something like template <typename T> void reset(T& t) { std::swap(t, T()); } (of course to be refined...) Stefan

why not : template <typename T> void reset(T& t) { t = T(); } but I don´t think this exists in boost already, but should. On Wed, 26 Jan 2005 16:48:41 +0100, Stefan Slapeta <stefan@slapeta.com> wrote:
Hi all,
do we have a non-member reset() somewhere? Something like
template <typename T> void reset(T& t) { std::swap(t, T()); }
(of course to be refined...)
Stefan
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

From: =?ISO-8859-1?Q?S=E9rgio_Vale_e_Pace?= <svpace.forum@gmail.com>
On Wed, 26 Jan 2005 16:48:41 +0100, Stefan Slapeta <stefan@slapeta.com> wrote:
do we have a non-member reset() somewhere? Something like
template <typename T> void reset(T& t) { std::swap(t, T()); }
(of course to be refined...)
why not :
template <typename T> void reset(T& t) { t = T(); }
swap() is typically as fast as or faster than copy assignment, so it is preferable to copy assignment. There are also types for which swap() exists and copy assignment doesn't. There are also those types with neither swap() nor copy assignment, but which do have a reset() member function. The point is that several versions will be needed and specialization will be needed, too. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Rob Stewart <stewart@sig.com> writes:
From: =?ISO-8859-1?Q?S=E9rgio_Vale_e_Pace?= <svpace.forum@gmail.com>
On Wed, 26 Jan 2005 16:48:41 +0100, Stefan Slapeta <stefan@slapeta.com> wrote:
do we have a non-member reset() somewhere? Something like
template <typename T> void reset(T& t) { std::swap(t, T()); }
(of course to be refined...)
why not :
template <typename T> void reset(T& t) { t = T(); }
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. -- Dave Abrahams Boost Consulting www.boost-consulting.com

David Abrahams wrote:
Rob Stewart <stewart@sig.com> writes:
From: =?ISO-8859-1?Q?S=E9rgio_Vale_e_Pace?= <svpace.forum@gmail.com>
On Wed, 26 Jan 2005 16:48:41 +0100, Stefan Slapeta <stefan@slapeta.com> wrote:
do we have a non-member reset() somewhere? Something like
template <typename T> void reset(T& t) { std::swap(t, T()); }
(of course to be refined...)
why not :
template <typename T> void reset(T& t) { t = T(); }
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.
What about exception safety guarantees of assignment vs. swap? Personally, I'd say these are more important to think about than speed... Regards, Daniel

Daniel Frey <d.frey@gmx.de> writes:
David Abrahams wrote:
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.
What about exception safety guarantees of assignment vs. swap?
What about 'em? Speaking generally, both are basic guarantee operations.
Personally, I'd say these are more important to think about than speed...
I'm not sure what you're driving at. -- Dave Abrahams Boost Consulting www.boost-consulting.com

David Abrahams wrote:
Daniel Frey <d.frey@gmx.de> writes:
David Abrahams wrote:
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.
What about exception safety guarantees of assignment vs. swap?
What about 'em? Speaking generally, both are basic guarantee operations.
I thought that swap is often (not always) a function which doesn't throw, e.g. containers' x.swap( y ). If it is, it would make reset a function with the strong instead of the basic exception guarantee. But then, this might not be general enough to be worth the trouble... OK, forget about it :) Regards, Daniel

Daniel Frey <d.frey@gmx.de> writes:
David Abrahams wrote:
Daniel Frey <d.frey@gmx.de> writes:
David Abrahams wrote:
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.
What about exception safety guarantees of assignment vs. swap? What about 'em? Speaking generally, both are basic guarantee operations.
I thought that swap is often (not always) a function which doesn't throw, e.g. containers' x.swap( y ).
Often == sometimes == never guaranteed to be. Same for operator=.
If it is, it would make reset a function with the strong instead of the basic exception guarantee.
But then, this might not be general enough to be worth the trouble... OK, forget about it :)
Premature strengthening of exception guarantees is a common source of inefficiency. -- Dave Abrahams Boost Consulting www.boost-consulting.com

Stefan Slapeta <stefan_nospam_@slapeta.com> writes:
David Abrahams wrote:
Not for many types that one might like to reset, such as pointers, shared_ptr<T>, and integral types.
So I guess you mean that plain assignment is better in general?
No, I mean that you can't make any clear generalization. It seems to me like for this particular case plain assignment is probably better. YMMV. -- Dave Abrahams Boost Consulting www.boost-consulting.com

Stefan Slapeta <stefan_nospam_@slapeta.com> writes:
David Abrahams wrote:
No, I mean that you can't make any clear generalization. It seems to me like for this particular case plain assignment is probably better. YMMV.
so do you think that a reset utility is a good idea or not? (in spite of YMMV :) )
I have no opinion. -- Dave Abrahams Boost Consulting www.boost-consulting.com

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;

Rob Stewart wrote: [...]
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.
Everybody is free to overload reset for his own types, which makes perfect sense e.g. for smart prts: template <typename T> void reset(shared_ptr<T> &p) { p.reset(); } ...is found via ADL Stefan

Stefan Slapeta wrote:
Everybody is free to overload reset for his own types, which makes perfect sense e.g. for smart prts:
template <typename T> void reset(shared_ptr<T> &p) { p.reset(); }
...is found via ADL
...and, if we had this kind of 'customized' reset, maybe Joel could be convinced to put a lazy reset into Phoenix 2 ;-) Stefan
participants (6)
-
Daniel Frey
-
David Abrahams
-
Rob Stewart
-
Stefan Slapeta
-
Stefan Slapeta
-
Sérgio Vale e Pace