Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-09-24 15:46:04


Daniel Wallin wrote:
>
> FWIW, here's the scopeguard implementation that I use:
>
> namespace detail
> {
> class guard_base
> {
> public:
> guard_base()
> : m_dismissed(false)
> {}
>
> void dismiss() const
> { m_dismissed = true; }
>
> protected:
> mutable bool m_dismissed;
> };
>
> template<class T>
> class guard_impl : public guard_base
> {
> public:
> explicit guard_impl(const T& x)
> : m_action(x)
> {}
>
> ~guard_impl()
> {
> if (!m_dismissed)
> m_action();
> }
>
> private:
> T m_action;
> };
> }
>
> template<class T>
> detail::guard_impl<T> make_guard(T x)
> {
> return detail::guard_impl<T>(x);
> }
>
> typedef const detail::guard_base& scope_guard;

You are assuming that

    scope_guard g = make_guard(f);

will only create a single instance of guard_impl<F> (no copies), but this is
not guaranteed.


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