Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Macro BOOST_SCOPE_EXIT_TPL

BOOST_SCOPE_EXIT_TPL — This macro is a workaround for various versions of GCC to declare scope exits within templates.

Synopsis

// In header: <boost/scope_exit.hpp>

BOOST_SCOPE_EXIT_TPL(capture_list)

Description

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.


PrevUpHomeNext