Proposal for standard library: std::reset

83 views
Skip to first unread message

Walt Karas

unread,
Jun 25, 2017, 2:24:31 PM6/25/17
to ISO C++ Standard - Future Proposals
The reference implementation would be:

template <typename T> reset(T &v) { T tmp{static_cast<T &&>(v)}; }

It releases any dynamically-allocated resources held by an object, leaving it in a safe state to be assigned or destroyed.

Motivation:  I want to communicate a value set in one thread and used in another.  I'm thinking to write a class template, with 2 variables to hold the value, and to use ping-pong buffering, to ease the atomicity issues of the setting thread creating a new value for the using thread.  I want to reset the old value when the using thread switches to the new value.  The reset will safely release any dynamically-allocated resources used by the old value, but be optimized out for values of simpler types that don't have dynamically allocated resources.

Daniel Krügler

unread,
Jun 25, 2017, 2:37:03 PM6/25/17
to std-pr...@isocpp.org
2017-06-25 20:24 GMT+02:00 'Walt Karas' via ISO C++ Standard - Future
Proposals <std-pr...@isocpp.org>:
> The reference implementation would be:
>
> template <typename T> reset(T &v) { T tmp{static_cast<T &&>(v)}; }
>
> It releases any dynamically-allocated resources held by an object, leaving
> it in a safe state to be assigned or destroyed.

What would it mean when T is a built-in pointer? How would reset
discriminate a pointer that owns or that doesn't own a resource? Is
reset to be intended to be a customization point? As you describe it
it sounds so trivial that I'm not sure why it would be worth
standardizing it. Could you elaborate a bit about realistic
optimization choices that could be realized by a Library
implementation but not by a third-party component that provides that
feature? Does there exists something like that in Boost or some other
library?

Thanks,

- Daniel

Walt Karas

unread,
Jun 25, 2017, 2:45:36 PM6/25/17
to ISO C++ Standard - Future Proposals
Primitive pointers owning resources is an unsolvable problem, with or without this.

I think you are right, the only potential value of standardizing this is it would be conveniently available to anyone with an installed compiler.

Nicol Bolas

unread,
Jun 25, 2017, 3:19:21 PM6/25/17
to ISO C++ Standard - Future Proposals
On Sunday, June 25, 2017 at 2:24:31 PM UTC-4, Walt Karas wrote:
The reference implementation would be:

template <typename T> reset(T &v) { T tmp{static_cast<T &&>(v)}; }

I think you're missing a `void` here.

It releases any dynamically-allocated resources held by an object, leaving it in a safe state to be assigned or destroyed.

Motivation:  I want to communicate a value set in one thread and used in another.  I'm thinking to write a class template, with 2 variables to hold the value, and to use ping-pong buffering, to ease the atomicity issues of the setting thread creating a new value for the using thread.  I want to reset the old value when the using thread switches to the new value.  The reset will safely release any dynamically-allocated resources used by the old value, but be optimized out for values of simpler types that don't have dynamically allocated resources.

I'm not sure I see the need for a generic algorithm for doing this. If you have some RAII object which may hold resources, you can try to empty it with `Typename(std::move(object));` just fine. If `Typename` is trivially copyable and moveable, then the compiler can know that this statement results in nothing. Sure, you'll have to know the typename, but that's at best a minor inconvenience.

I don't think this sort of thing comes up enough to warrant such a function.
Reply all
Reply to author
Forward
0 new messages