相关疑难解决方法(0)

最简单,最新的c ++ 11 ScopeGuard

我正在尝试编写一个基于Alexandrescu概念但使用c ++ 11习语的简单ScopeGuard.

namespace RAII
{
    template< typename Lambda >
    class ScopeGuard
    {
        mutable bool committed;
        Lambda rollbackLambda; 
        public:

            ScopeGuard( const Lambda& _l) : committed(false) , rollbackLambda(_l) {}

            template< typename AdquireLambda >
            ScopeGuard( const AdquireLambda& _al , const Lambda& _l) : committed(false) , rollbackLambda(_l)
            {
                _al();
            }

            ~ScopeGuard()
            {
                if (!committed)
                    rollbackLambda();
            }
            inline void commit() const { committed = true; }
    };

    template< typename aLambda , typename rLambda>
    const ScopeGuard< rLambda >& makeScopeGuard( const aLambda& …
Run Code Online (Sandbox Code Playgroud)

c++ lambda scopeguard exception-safety c++11

31
推荐指数
8
解决办法
3万
查看次数

标签 统计

c++ ×1

c++11 ×1

exception-safety ×1

lambda ×1

scopeguard ×1