Boost logo

Boost :

Subject: [boost] [scope_exit] can MSVC lambdas capture data members?
From: lcaminiti (lorcaminiti_at_[hidden])
Date: 2012-03-14 14:44:31

Hello all,

Can anyone try the following code on a MSVC compiler with lambdas (MSVC10?)?

#include <vector>

struct person {};

struct world {
    void add_person(person const& a_person) {
        bool commit = false;

        auto scope_exit = [&commit, &persons_]() mutable -> void {
            if(!commit) persons_.pop_back();

        commit = true;

    std::vector<person> persons_;

int main() {
    world w;
    person p;
    return 0;

This code compiles on GCC 4.5.3 with C++0x extensions (including lambdas)
but based on some errors I'm seeing in trunk's ScopeExit regressions tests,
I'm guessing it will fail using MSVC lambdas with errors similar to the

error C3480: 'world::persons_': a lambda capture variable must be from an
enclosing function scope
warning C4573: the usage of 'world::persons_' requires the compiler to
capture 'this' but the current default capture mode does not allow it

Unfortunately, I don't have a MSVC compiler with lambdas so thank you very
much for trying my code!

View this message in context:
Sent from the Boost - Dev mailing list archive at

Boost list run by bdawes at, gregod at, cpdaniel at, john at