N3830: Scoped Resource - Generic RAII Wrapper for the Standard Library -- P Sommerlad, A Sandoval

A new WG21 paper is available. A copy is linked below, and the paper will also appear in the next normal WG21 mailing. If you are not a committee member, please use the comments section below or the std-proposals forum for public discussion.

Document number: N3830

Date: 2013-12-23

Scoped Resource -- Generic RAII Wrapper for the Standard Library

by Peter Sommerlad and Andrew L. Sandoval

Excerpt:

Changes from N367: Replace all 4 proposed classes with a single class covering all use cases, using variadic templates, as determined in the Fall 2013 LEWG meeting.

The Standard Template Library provides RAII classes for managing pointer types, such as std::unique_ptr and std::shared_ptr. This proposal seeks to add a new generic RAII class which ties zero or more resources to a clean-up/completion routine which is bound by scope, ensuring execution at scope exit (as the object is destroyed) unless released or executed early or returned by moving its value.

Add a Comment

Comments are closed.

Comments (1)

0 0

Adi said on Jan 24, 2014 11:45 AM:

I've been longing for this for a long time. I look forward to seeing it in the standard.

How would scoped_resource handle release functions that take the address-of the resource?
I've seen many APIs that take a ** so they can set the ptr to NULL. For example see this: http://docs.opencv.org/modules/core/doc/old_basic_structures.html?highlight=cvreleaseimage#releaseimage

I guess you could do it with a lambda but then you actually need a way to reference the resource itself to take its address.
Also, writing a lambda every time is tedious and error prone as this is quite a common API approach.

Adi