File: scope.rst

package info (click to toggle)
libhx 5.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,664 kB
  • sloc: ansic: 10,332; sh: 5,230; cpp: 133; makefile: 116
file content (22 lines) | stat: -rw-r--r-- 570 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
============
Scope guards
============

``scope_exit``
==============

scope_exit creates an object that runs a predefined function when a scope ends.
This is useful for augmenting C APIs with something of a destructor without
wrapping the stuff in an explicit class of its own. For instance,

.. code-block:: c++

	#include <libHX/option.h>
	#include <libHX/scope.hpp>
	int main() {
		auto fa = HXformat_init();
		if (fa == nullptr)
			return 0;
		auto cleanup_fa = HX::make_scope_exit([&]() { HXformat_free(fa); });
		HXformat_add(fa, "foo", "bar", HXTYPE_STRING);
	}