summaryrefslogtreecommitdiff
path: root/doc/scope.rst
blob: 84ad69bbf30530cdadb122f4fe960810d7a426e3 (plain)
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);
	}