Reference
Scope exits allow to execute arbitrary code when the enclosing scope exits. This macro declares a scope exit. The scope exit declaration schedules the execution of the scope exit body at the exit of the enclosing scope:{ // Some local scope. ... BOOST_SCOPE_EXIT(capture_list) { ... // Body code. } BOOST_SCOPE_EXIT_END ... } The enclosing scope must be local. If multiple scope exits are declared within the same enclosing scope, the scope exit bodies are executed in the reversed order of their declarations. Note how the end of the scope exit body must be marked by BOOST_SCOPE_EXIT_END.Parameters: capture_listOn compilers that support variadic macros (see also Boost.Config BOOST_NO_CXX11_VARIADIC_MACROS), the capture list syntax is defined by the following grammar: capture_list: void | capture_tuple | capture_sequence capture_tuple: capture, capture, ... capture_sequence: (capture) (capture) ... capture: [&]variable | this_ On compilers that do not support variadic macros, capture_tuple cannot be used: capture_list: void | capture_sequence Furthermore, if BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS is defined on C++11 compilers that support lambda functions (i.e., Boost.Config's BOOST_NO_CXX11_LAMBDAS is not defined) then a semicolon ; can be used instead of BOOST_SCOPE_EXIT_END and this can be used instead of this_: capture: [&]variable | this_ | this (Lexical conventions: token1 | token2 means either token1 or token2; [token] means either token or nothing; {expression} means the tokens resulting from the expression.) Note that on compilers that support variadic macros (most of moder compliers and all C++11 compilers), the capture list can be specified as a comma-separated list of tokens (this is the preferred syntax). However, on all compilers the same macro BOOST_SCOPE_EXIT also allows to specify the capture list as a Boost.Preprocessor sequence of tokens (for supporting compilers without variadic macros and for backward compatibility with older versions of this library).The name variable of each captured variable must be a valid name in the enclosing scope and it must appear exactly once in the capture list. If a capture starts with the ampersand sign &, the corresponding variable will be available by reference within the scope exit body; otherwise, a copy of the variable will be made at the point of the scope exit declaration and that copy will be available inside the scope exit body (in this case, the variable's type must be CopyConstructible).From within a member function, the object this can be captured using the special name this_ in both the capture list and the scope exit body (using this instead of this_ in the scope exit body leads to undefined behaviour).It is possible to capture no variable by specifying the capture list as void (regardless of variadic macro support).Only variables listed in the capture list, static variables, extern variables, global variables, functions, and enumerations from the enclosing scope can be used inside the scope exit body.On various GCC versions the special macro BOOST_SCOPE_EXIT_TPL must be used instead of BOOST_SCOPE_EXIT within templates (to maximize portability, it is recommended to always use BOOST_SCOPE_EXIT_TPL within templates).On C++11, it is possible capture all variables in scope without listing their names one-by-one using the macro BOOST_SCOPE_EXIT_ALL.In general, the special macro BOOST_SCOPE_EXIT_ID must be used instead of BOOST_SCOPE_EXIT when it is necessary to expand multiple scope exit declarations on the same line.Warning: The implementation executes the scope exit body within a destructor thus the scope exit body must never throw in order to comply with STL exception safety requirements.Note: The implementation uses Boost.Typeof to automatically deduce the types of the captured variables. In order to compile code in type-of emulation mode, all types must be properly registered with Boost.Typeof (see the Getting Started section).See: Tutorial section, Getting Started section, No Variadic Macros section, BOOST_SCOPE_EXIT_TPL, BOOST_SCOPE_EXIT_ALL, BOOST_SCOPE_EXIT_END, BOOST_SCOPE_EXIT_ID. This macro is a workaround for various versions of GCC to declare scope exits within templates. Various versions of the GCC compiler do not compile BOOST_SCOPE_EXIT inside function templates. As a workaround, BOOST_SCOPE_EXIT_TPL should be used instead of BOOST_SCOPE_EXIT in these cases:{ // Some local scope. ... BOOST_SCOPE_EXIT_TPL(capture_list) { ... // Body code. } BOOST_SCOPE_EXIT_END ... } The syntax of BOOST_SCOPE_EXIT_TPL is the exact same as the one of BOOST_SCOPE_EXIT (see BOOST_SCOPE_EXIT for more information).On C++11 compilers, BOOST_SCOPE_EXIT_TPL is not needed because BOOST_SCOPE_EXIT always compiles on GCC versions that support C++11. However, BOOST_SCOPE_EXIT_TPL is still provided on C++11 so to write code that is portable between C++03 and C++11 compilers. It is recommended to always use BOOST_SCOPE_EXIT_TPL within templates so to maximize portability.In general, the special macro BOOST_SCOPE_EXIT_ID_TPL must be used instead of BOOST_SCOPE_EXIT_TPL when it is necessary to expand multiple scope exit declarations on the same line within templates.Note: The issue in compiling scope exit declarations that some GCC versions have is illustrated by the following code (see also GCC bug 37920): template<class T> void f(T const& x) { int i = 0; struct local { typedef __typeof__(i) typeof_i; typedef __typeof__(x) typeof_x; }; typedef local::typeof_i i_type; typedef local::typeof_x x_type; } int main(void) { f(0); } This can be fixed by adding typename in front of local::typeof_i and local::typeof_x (which is the approach followed by the implementation of the BOOST_SCOPE_EXIT_TPL macro).Note: Although BOOST_SCOPE_EXIT_TPL has the same suffix as BOOST_TYPEOF_TPL, it does not follow the Boost.Typeof convention.See: Tutorial section, BOOST_SCOPE_EXIT, BOOST_SCOPE_EXIT_END, BOOST_SCOPE_EXIT_ID_TPL. This macro allows to expand multiple scope exit declarations on the same line. This macro is equivalent to BOOST_SCOPE_EXIT but it can be expanded multiple times on the same line if different identifiers id are provided for each expansion (see BOOST_SCOPE_EXIT for more information).Parameters: idA unique identifier token which can be concatenated by the preprocessor (LINE, scope_exit_number_1_on_line_123, a combination of alphanumeric tokens, etc). capture_listSame as the capture_list parameter of the BOOST_SCOPE_EXIT macro. Note: This macro can be useful when the scope exit macros are expanded within user-defined macros (because nested macros expand on the same line). On some compilers (e.g., MSVC which supports the non standard COUNTER macro) it might not be necessary to use this macro but the use of this macro is always necessary to ensure portability when expanding multiple scope exit declarations on the same line.See: Tutorial section, BOOST_SCOPE_EXIT, BOOST_SCOPE_EXIT_END_ID, BOOST_SCOPE_EXIT_ALL_ID, BOOST_SCOPE_EXIT_ID_TPL. This macro is required to expand multiple scope exit declarations on the same line within templates on various versions of GCC. This macro is equivalent to BOOST_SCOPE_EXIT_TPL but it can be expanded multiple times on the same line if different identifiers id are provided for each expansion (see BOOST_SCOPE_EXIT_TPL for more information). As with BOOST_SCOPE_EXIT_TPL, it is recommended to always use this macro when expanding scope exits multiple times on the same line within templates.Parameters: idA unique identifier token which can be concatenated by the preprocessor (LINE, scope_exit_number_1_on_line_123, a combination of alphanumeric tokens, etc). capture_listSame as the capture_list parameter of the BOOST_SCOPE_EXIT_TPL macro. Note: This macro can be useful when the scope exit macros are expanded within user-defined macros (because nested macros expand on the same line). On some compilers (e.g., MSVC which supports the non standard COUNTER macro) it might not be necessary to use this macro but the use of this macro is always necessary to ensure portability when expanding multiple scope exit declarations on the same line.See: Tutorial section, BOOST_SCOPE_EXIT_TPL, BOOST_SCOPE_EXIT_END_ID, BOOST_SCOPE_EXIT_ID, BOOST_SCOPE_EXIT_ALL_ID. This macro declares a scope exit that captures all variables in scope (C++11 only). This macro accepts a capture list starting with either & or = to capture all variables in scope by reference or value respectively (following the same syntax of C++11 lambdas). A part from that, this macro works like BOOST_SCOPE_EXIT (see BOOST_SCOPE_EXIT for more information):{ // Some local scope. ... BOOST_SCOPE_EXIT_ALL(capture_list) { // C++11 only. ... // Body code. }; // Use `;` instead of `BOOST_SCOPE_EXIT_END` (C++11 only). ... } Note how the end of the scope exit body declared by this macro must be marked by a semi-column ; (and not by BOOST_SCOPE_EXIT_END).Warning: This macro is only available on C++11 compilers (specifically, on C++11 compilers that do not define the Boost.Config BOOST_NO_CXX11_LAMBDAS macro). It is not defined on non-C++11 compilers so its use on non-C++11 compilers will generate a compiler error.Parameters: capture_listOn compilers that support variadic macros (see also Boost.Config BOOST_NO_CXX11_VARIADIC_MACROS), the capture list syntax is defined by the following grammar: capture_list: capture_tuple | capture_sequence capture_tuple: {& | =} [, capture, capture, ...] capture_sequence: {(&) | (=)} [(capture) (capture) ...] capture: [&]variable | this_ On compilers that do not support variadic macros, capture_tuple cannot be used: capture_list: void | capture_sequence Furthermore, on C++11 compilers that support the use of typename outside templates, also this can be used to capture the object at member function scope: capture: [&]variable | this_ | this (Lexical conventions: token1 | token2 means either token1 or token2; [token] means either token or nothing; {expression} means the token resulting from the expression.) Note that on compilers with variadic macro support (which should be all C++11 compilers), the capture list can be specified as a comma-separated list. On all compilers, the same macro BOOST_SCOPE_EXIT_ALL also allows to specify the capture list as a Boost.Preprocessor sequence.The capture list must always contain at least the leading & or = so it can never be void (BOOST_SCOPE_EXIT(void) should be used to program scope exits with an empty capture list).In general, the special macro BOOST_SCOPE_EXIT_ALL_ID must be used instead of BOOST_SCOPE_EXIT_ALL when it is necessary to expand multiple scope exit declarations on the same line.Warning: This macro capture list follows the exact same syntax of C++11 lambda captures which is unfortunately different from the syntax of BOOST_SCOPE_EXIT captures (unless programmers define the BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS macro). For example, like C++11 lambda functions, BOOST_SCOPE_EXIT_ALL requires to capture data members by capturing the object this while BOOST_SCOPE_EXIT allows to capture data members directly and without capturing the object.Warning: The implementation executes the scope exit body within a destructor thus the scope exit body must never throw in order to comply with STL exception safety requirements.Note: This macro can always be used also within templates (so there is no need for a BOOST_SCOPE_EXIT_ALL_TPL macro).See: Tutorial section, No Variadic Macros section, BOOST_SCOPE_EXIT, BOOST_SCOPE_EXIT_ALL_ID. This macro allows to expand on the same line multiple scope exits that capture all variables in scope (C++11 only). This macro is equivalent to BOOST_SCOPE_EXIT_ALL but it can be expanded multiple times on the same line if different identifiers id are provided for each expansion (see BOOST_SCOPE_EXIT_ALL for more information). As with BOOST_SCOPE_EXIT_ALL, this macro is only available on C++11 compilers (specifically, on C++11 compilers that do not define the Boost.Config BOOST_NO_CXX11_LAMBDAS macro).Parameters: idA unique identifier token which can be concatenated by the preprocessor (LINE, scope_exit_number_1_on_line_123, a combination of alphanumeric tokens, etc). capture_listSame as the capture_list parameter of the BOOST_SCOPE_EXIT_ALL macro. Note: This macro can be useful when the scope exit macros are expanded within user-defined macros (because nested macros expand on the same line). On some compilers (e.g., MSVC which supports the non standard COUNTER macro) it might not be necessary to use this macro but the use of this macro is always necessary to ensure portability when expanding multiple scope exit declarations on the same line.See: Tutorial section, BOOST_SCOPE_EXIT_ALL, BOOST_SCOPE_EXIT_ID. This macro marks the end of a scope exit body. This macro must follow the closing curly bracket } that ends the body of either BOOST_SCOPE_EXIT or BOOST_SCOPE_EXIT_TPL:{ // Some local scope. ... BOOST_SCOPE_EXIT(capture_list) { ... // Body code. } BOOST_SCOPE_EXIT_END ... } In general, the special macro BOOST_SCOPE_EXIT_END_ID must be used instead of BOOST_SCOPE_EXIT_END when it is necessary to expand multiple scope exit bodies on the same line.Note: If programmers define the BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS macro on C++11 compilers, a semicolon ; can be used instead of this macro. However, to maximize portability, it is recommended to always use BOOST_SCOPE_EXIT_END.See: Tutorial section, BOOST_SCOPE_EXIT, BOOST_SCOPE_EXIT_TPL, BOOST_SCOPE_EXIT_END_ID. This macro allows to terminate multiple scope exit bodies on the same line. This macro is equivalent to BOOST_SCOPE_EXIT_END but it can be expanded multiple times on the same line if different identifiers id are provided for each expansion (see BOOST_SCOPE_EXIT_END for more information).Parameters: idA unique identifier token which can be concatenated by the preprocessor (LINE, scope_exit_number_1_on_line_123, a combination of alphanumeric tokens, etc). Note: This macro can be useful when the scope exit macros are expanded within user-defined macros (because macros all expand on the same line). On some compilers (e.g., MSVC which supports the non standard COUNTER macro) it might not be necessary to use this macro but the use of this macro is always necessary to ensure portability when expanding multiple scope exit macros on the same line (because this library can only portably use LINE to internally generate unique identifiers).See: BOOST_SCOPE_EXIT_ID, BOOST_SCOPE_EXIT_ID_TPL, BOOST_SCOPE_EXIT_END. Force to use C++11 lambda functions to implement scope exits. If programmers define this configuration macro on a C++11 compiler for which the Boost.Config macro BOOST_NO_CXX11_LAMBDAS is not defined, the BOOST_SCOPE_EXIT and BOOST_SCOPE_EXIT_TPL macros will use C++11 lambda functions to declare scope exits. By default this macro is not defined.Warning: When scope exits are implemented using lambda functions, the syntax of the capture list follows the exact same syntax of C++11 lambda captures which is in general different from the legacy capture syntax of this library. For example, C++11 lambdas require to capture data members by capturing the object this while this library always allowed to capture data members directly. Therefore, when this configuration macro is defined, BOOST_SCOPE_EXIT and BOOST_SCOPE_EXIT_TPL are no longer backward compatible (and this is why this macro is not defined by default).A semicolon ; can be used instead of BOOST_SCOPE_EXIT_END when this configuration macro is defined (but it is recommended to always use BOOST_SCOPE_EXIT_END so to maximize portability).Note: This configuration macro does not control the definition of BOOST_SCOPE_EXIT_ALL which is always and automatically defined on compilers that support C++11 lambda functions.See: BOOST_SCOPE_EXIT, BOOST_SCOPE_EXIT_TPL, BOOST_SCOPE_EXIT_END.