Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2007-04-05 00:47:00


AMDG

Hans Larsen <hans <at> poltras.com> writes:

>
>
> Hey guys. Very interesting this problem is and the proposed
> solutions leads one to another :)
>
> Problem underlined below.
>
> How do you know that it is args_line1? You cannot use __LINE__,
> since it is == 5.

Yeah. Wasn't thinking. Here's a solution that ought to work

struct scope_exit_helper {
    virtual ~scope_exit_helper() {}
};

struct scope_exit_helper_test : scope_exit_helper {
    void *dummy;
};

    int* i = new int;
    struct scope_exit_helper1_line1_t {int*& i; }
    scope_exit_helper1_line1 = { i };
    struct scope_exit_helper2_line1_t {
        ~scope_exit_helper2_line1_t() {
            helper->~scope_exit_helper();
        }
        scope_exit_helper* helper;
        boost::aligned_storage<sizeof(scope_exit_helper_test)> storage;
    } scope_exit_helper2_line1;
    {
        scope_exit_helper1_line1_t* in = &scope_exit_helper1_line1;
        scope_exit_helper2_line1_t* out = &scope_exit_helper2_line1;
        struct scope_exit_t : scope_exit_helper {
            scope_exit_t(scope_exit_helper1_line1_t* p) : impl(p) {}
            scope_exit_helper1_line1_t* impl;
            ~scope_exit_t() {
                run(impl->i);
            }
            void run(int*& i)

            {
                delete i;
                std::cout << "block exited" << std::endl;
            }

        }* scope_exit = static_cast<scope_exit_t*>(
                  static_cast<void*>(out->storage.address()));
        out->helper = scope_exit;
        new(scope_exit) scope_exit_t(in);
        BOOST_STATIC_ASSERT((sizeof(scope_exit_t) ==
                   sizeof(scope_exit_helper_test)));
    }

In Christ,
Steven Watanabe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk