Skip to content

Commit db963ea

Browse files
committed
Fixed an example of using scope_exit with std::function in the docs.
Closes #20.
1 parent eea343a commit db963ea

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

doc/scope_guards.qbk

+10-6
Original file line numberDiff line numberDiff line change
@@ -600,25 +600,29 @@ It is possible to use scope guard classes to implement scope exit actions that a
600600
a function object wrapper such as `std::function` together with the scope guard to schedule the function call. For example:
601601

602602
using cleanup_func_t = std::function< void() >;
603+
cleanup_func_t cleanup_func;
603604
// Create an inactive scope guard first, since the cleanup function is not set yet
604-
boost::scope::scope_exit< cleanup_func_t > cleanup(cleanup_func_t(), false);
605+
boost::scope::scope_exit< cleanup_func_t& > cleanup(cleanup_func, false);
605606

606-
// Later in the program, initialize the scope guard with the function selected at run time
607+
// Later in the program, initialize the cleanup function with the function selected at run time
607608
if (cond)
608609
{
609-
cleanup = boost::scope::scope_exit< cleanup_func_t >([]
610+
cleanup_func = []
610611
{
611612
std::cout << "cond is true" << std::endl;
612-
});
613+
};
613614
}
614615
else
615616
{
616-
cleanup = boost::scope::scope_exit< cleanup_func_t >([]
617+
cleanup_func = []
617618
{
618619
std::cout << "cond is false" << std::endl;
619-
});
620+
};
620621
}
621622

623+
// Activate the scope guard once the cleanup function is initialized
624+
cleanup.set_active(true);
625+
622626
It is also possible to do this with `BOOST_SCOPE_DEFER`, although it eliminates one of the advantages provided by this macro, namely not
623627
having to invent a variable name. Also note that the function wrapper must be valid at all times once the scope guard is constructed.
624628

0 commit comments

Comments
 (0)