1. <small id='FwmJO'></small><noframes id='FwmJO'>

    2. <i id='FwmJO'><tr id='FwmJO'><dt id='FwmJO'><q id='FwmJO'><span id='FwmJO'><b id='FwmJO'><form id='FwmJO'><ins id='FwmJO'></ins><ul id='FwmJO'></ul><sub id='FwmJO'></sub></form><legend id='FwmJO'></legend><bdo id='FwmJO'><pre id='FwmJO'><center id='FwmJO'></center></pre></bdo></b><th id='FwmJO'></th></span></q></dt></tr></i><div id='FwmJO'><tfoot id='FwmJO'></tfoot><dl id='FwmJO'><fieldset id='FwmJO'></fieldset></dl></div>

      <tfoot id='FwmJO'></tfoot>
        <bdo id='FwmJO'></bdo><ul id='FwmJO'></ul>

      <legend id='FwmJO'><style id='FwmJO'><dir id='FwmJO'><q id='FwmJO'></q></dir></style></legend>
    3. 最简单最简洁的c++11 ScopeGuard

      The simplest and neatest c++11 ScopeGuard(最简单最简洁的c++11 ScopeGuard)
      <i id='KdvXp'><tr id='KdvXp'><dt id='KdvXp'><q id='KdvXp'><span id='KdvXp'><b id='KdvXp'><form id='KdvXp'><ins id='KdvXp'></ins><ul id='KdvXp'></ul><sub id='KdvXp'></sub></form><legend id='KdvXp'></legend><bdo id='KdvXp'><pre id='KdvXp'><center id='KdvXp'></center></pre></bdo></b><th id='KdvXp'></th></span></q></dt></tr></i><div id='KdvXp'><tfoot id='KdvXp'></tfoot><dl id='KdvXp'><fieldset id='KdvXp'></fieldset></dl></div>
    4. <tfoot id='KdvXp'></tfoot>
        • <bdo id='KdvXp'></bdo><ul id='KdvXp'></ul>

              <small id='KdvXp'></small><noframes id='KdvXp'>

              <legend id='KdvXp'><style id='KdvXp'><dir id='KdvXp'><q id='KdvXp'></q></dir></style></legend>
                <tbody id='KdvXp'></tbody>

                本文介绍了最简单最简洁的c++11 ScopeGuard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                I'm attempting to write a simple ScopeGuard based on Alexandrescu concepts but with c++11 idioms.

                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& _a , const rLambda& _r)
                    {
                        return ScopeGuard< rLambda >( _a , _r );
                    }
                
                    template<typename rLambda>
                    const ScopeGuard< rLambda >& makeScopeGuard(const rLambda& _r)
                    {
                        return ScopeGuard< rLambda >(_r );
                    }
                }
                

                用法如下:

                void SomeFuncThatShouldBehaveAtomicallyInCaseOfExceptions() 
                {
                   std::vector<int> myVec;
                   std::vector<int> someOtherVec;
                
                   myVec.push_back(5);
                   //first constructor, adquire happens elsewhere
                   const auto& a = RAII::makeScopeGuard( [&]() { myVec.pop_back(); } );  
                
                   //sintactically neater, since everything happens in a single line
                   const auto& b = RAII::makeScopeGuard( [&]() { someOtherVec.push_back(42); }
                                     , [&]() { someOtherVec.pop_back(); } ); 
                
                   b.commit();
                   a.commit();
                }
                

                由于我的版本比那里的大多数示例(如 Boost ScopeExit)短得多,我想知道我遗漏了哪些专业.希望我在这里处于 80/20 场景中(我用 20% 的代码行获得了 80% 的整洁度),但我不禁想知道我是否遗漏了一些重要的东西,或者是否有一些值得的缺点提到这个版本的 ScopeGuard 习语

                Since my version is way shorter than most examples out there (like Boost ScopeExit) i'm wondering what specialties i'm leaving out. Hopefully i'm in a 80/20 scenario here (where i got 80 percent of neatness with 20 percent of lines of code), but i couldn't help but wonder if i'm missing something important, or is there some shortcoming worth mentioning of this version of the ScopeGuard idiom

                谢谢!

                编辑 我注意到 makeScopeGuard 的一个非常重要的问题,它在构造函数中采用了 adquire lambda.如果 adquire lambda 抛出,则永远不会调用 release lambda,因为从未完全构造过作用域守卫.在许多情况下,这是所需的行为,但我觉得有时也需要一个在发生抛出时调用回滚的版本:

                Edit I noticed a very important issue with the makeScopeGuard that takes the adquire lambda in the constructor. If the adquire lambda throws, then the release lambda is never called, because the scope guard was never fully constructed. In many cases, this is the desired behavior, but i feel that sometimes a version that will invoke rollback if a throw happens is desired as well:

                //WARNING: only safe if adquire lambda does not throw, otherwise release lambda is never invoked, because the scope guard never finished initialistion..
                template< typename aLambda , typename rLambda>
                ScopeGuard< rLambda > // return by value is the preferred C++11 way.
                makeScopeGuardThatDoesNOTRollbackIfAdquireThrows( aLambda&& _a , rLambda&& _r) // again perfect forwarding
                {
                    return ScopeGuard< rLambda >( std::forward<aLambda>(_a) , std::forward<rLambda>(_r )); // *** no longer UB, because we're returning by value
                }
                
                template< typename aLambda , typename rLambda>
                ScopeGuard< rLambda > // return by value is the preferred C++11 way.
                makeScopeGuardThatDoesRollbackIfAdquireThrows( aLambda&& _a , rLambda&& _r) // again perfect forwarding
                {
                    auto scope = ScopeGuard< rLambda >(std::forward<rLambda>(_r )); // *** no longer UB, because we're returning by value
                    _a();
                    return scope;
                }
                

                <小时>

                所以为了完整起见,我想在这里放上完整的代码,包括测试:


                so for completeness, i want to put in here the complete code, including tests:

                #include <vector>
                
                namespace RAII
                {
                
                    template< typename Lambda >
                    class ScopeGuard
                    {
                        bool committed;
                        Lambda rollbackLambda; 
                        public:
                
                            ScopeGuard( const Lambda& _l) : committed(false) , rollbackLambda(_l) {}
                
                            ScopeGuard( const ScopeGuard& _sc) : committed(false) , rollbackLambda(_sc.rollbackLambda) 
                            {
                                if (_sc.committed)
                                   committed = true;
                                else
                                   _sc.commit();
                            }
                
                            ScopeGuard( ScopeGuard&& _sc) : committed(false) , rollbackLambda(_sc.rollbackLambda)
                            {
                                if (_sc.committed)
                                   committed = true;
                                else
                                   _sc.commit();
                            }
                
                            //WARNING: only safe if adquire lambda does not throw, otherwise release lambda is never invoked, because the scope guard never finished initialistion..
                            template< typename AdquireLambda >
                            ScopeGuard( const AdquireLambda& _al , const Lambda& _l) : committed(false) , rollbackLambda(_l)
                            {
                               std::forward<AdquireLambda>(_al)();
                            }
                
                            //WARNING: only safe if adquire lambda does not throw, otherwise release lambda is never invoked, because the scope guard never finished initialistion..
                            template< typename AdquireLambda, typename L >
                            ScopeGuard( AdquireLambda&& _al , L&& _l) : committed(false) , rollbackLambda(std::forward<L>(_l))
                            {
                                std::forward<AdquireLambda>(_al)(); // just in case the functor has &&-qualified operator()
                            }
                
                
                            ~ScopeGuard()
                            {
                                if (!committed)
                                    rollbackLambda();
                            }
                            inline void commit() { committed = true; }
                    };
                
                
                    //WARNING: only safe if adquire lambda does not throw, otherwise release lambda is never invoked, because the scope guard never finished initialistion..
                    template< typename aLambda , typename rLambda>
                    ScopeGuard< rLambda > // return by value is the preferred C++11 way.
                    makeScopeGuardThatDoesNOTRollbackIfAdquireThrows( aLambda&& _a , rLambda&& _r) // again perfect forwarding
                    {
                        return ScopeGuard< rLambda >( std::forward<aLambda>(_a) , std::forward<rLambda>(_r )); // *** no longer UB, because we're returning by value
                    }
                
                    template< typename aLambda , typename rLambda>
                    ScopeGuard< rLambda > // return by value is the preferred C++11 way.
                    makeScopeGuardThatDoesRollbackIfAdquireThrows( aLambda&& _a , rLambda&& _r) // again perfect forwarding
                    {
                        auto scope = ScopeGuard< rLambda >(std::forward<rLambda>(_r )); // *** no longer UB, because we're returning by value
                        _a();
                        return scope;
                    }
                
                    template<typename rLambda>
                    ScopeGuard< rLambda > makeScopeGuard(rLambda&& _r)
                    {
                        return ScopeGuard< rLambda >( std::forward<rLambda>(_r ));
                    }
                
                    namespace basic_usage
                    {
                        struct Test
                        {
                
                            std::vector<int> myVec;
                            std::vector<int> someOtherVec;
                            bool shouldThrow;
                            void run()
                            {
                                shouldThrow = true;
                                try
                                {
                                    SomeFuncThatShouldBehaveAtomicallyInCaseOfExceptionsUsingScopeGuardsThatDoesNOTRollbackIfAdquireThrows();
                                } catch (...)
                                {
                                    AssertMsg( myVec.size() == 0 && someOtherVec.size() == 0 , "rollback did not work");
                                }
                                shouldThrow = false;
                                SomeFuncThatShouldBehaveAtomicallyInCaseOfExceptionsUsingScopeGuardsThatDoesNOTRollbackIfAdquireThrows();
                                AssertMsg( myVec.size() == 1 && someOtherVec.size() == 1 , "unexpected end state");
                                shouldThrow = true;
                                myVec.clear(); someOtherVec.clear();  
                                try
                                {
                                    SomeFuncThatShouldBehaveAtomicallyInCaseOfExceptionsUsingScopeGuardsThatDoesRollbackIfAdquireThrows();
                                } catch (...)
                                {
                                    AssertMsg( myVec.size() == 0 && someOtherVec.size() == 0 , "rollback did not work");
                                }
                            }
                
                            void SomeFuncThatShouldBehaveAtomicallyInCaseOfExceptionsUsingScopeGuardsThatDoesNOTRollbackIfAdquireThrows() //throw()
                            {
                
                                myVec.push_back(42);
                                auto a = RAII::makeScopeGuard( [&]() { HAssertMsg( myVec.size() > 0 , "attempt to call pop_back() in empty myVec"); myVec.pop_back(); } );  
                
                                auto b = RAII::makeScopeGuardThatDoesNOTRollbackIfAdquireThrows( [&]() { someOtherVec.push_back(42); }
                                                    , [&]() { HAssertMsg( myVec.size() > 0 , "attempt to call pop_back() in empty someOtherVec"); someOtherVec.pop_back(); } );
                
                                if (shouldThrow) throw 1; 
                
                                b.commit();
                                a.commit();
                            }
                
                            void SomeFuncThatShouldBehaveAtomicallyInCaseOfExceptionsUsingScopeGuardsThatDoesRollbackIfAdquireThrows() //throw()
                            {
                                myVec.push_back(42);
                                auto a = RAII::makeScopeGuard( [&]() { HAssertMsg( myVec.size() > 0 , "attempt to call pop_back() in empty myVec"); myVec.pop_back(); } );  
                
                                auto b = RAII::makeScopeGuardThatDoesRollbackIfAdquireThrows( [&]() { someOtherVec.push_back(42); if (shouldThrow) throw 1; }
                                                    , [&]() { HAssertMsg( myVec.size() > 0 , "attempt to call pop_back() in empty someOtherVec"); someOtherVec.pop_back(); } );
                
                                b.commit();
                                a.commit();
                            }
                        };
                    }
                }
                

                推荐答案

                Boost.ScopeExit 是一个需要处理非 C++11 代码的宏,即无法访问该语言中 lambdas 的代码.它使用了一些巧妙的模板技巧(例如滥用由将 < 用于模板和比较运算符而产生的歧义!)和预处理器来模拟 lambda 功能.这就是代码较长的原因.

                Boost.ScopeExit is a macro that needs to work with non-C++11 code, i.e. code that has no access to lambdas in the language. It uses some clever template hacks (like abusing the ambiguity that arises from using < for both templates and comparison operators!) and the preprocessor to emulate lambda features. That's why the code is longer.

                显示的代码也有问题(这可能是使用现有解决方案的最有力的原因):由于返回对临时文件的引用,它调用了未定义的行为.

                The code shown is also buggy (which is probably the strongest reason to use an existing solution): it invokes undefined behaviour due to returning references to temporaries.

                由于您尝试使用 C++11 功能,因此可以通过使用移动语义、右值引用和完美转发来大大改进代码:

                Since you're trying to use C++11 features, the code could be improved a lot by using move semantics, rvalue references and perfect-forwarding:

                template< typename Lambda >
                class ScopeGuard
                {
                    bool committed; // not mutable
                    Lambda rollbackLambda; 
                    public:
                
                
                        // make sure this is not a copy ctor
                        template <typename L,
                                  DisableIf<std::is_same<RemoveReference<RemoveCv<L>>, ScopeGuard<Lambda>>> =_
                        >
                        /* see http://loungecpp.net/w/EnableIf_in_C%2B%2B11
                         * and http://stackoverflow.com/q/10180552/46642 for info on DisableIf
                         */
                        explicit ScopeGuard(L&& _l)
                        // explicit, unless you want implicit conversions from *everything*
                        : committed(false)
                        , rollbackLambda(std::forward<L>(_l)) // avoid copying unless necessary
                        {}
                
                        template< typename AdquireLambda, typename L >
                        ScopeGuard( AdquireLambda&& _al , L&& _l) : committed(false) , rollbackLambda(std::forward<L>(_l))
                        {
                            std::forward<AdquireLambda>(_al)(); // just in case the functor has &&-qualified operator()
                        }
                
                        // move constructor
                        ScopeGuard(ScopeGuard&& that)
                        : committed(that.committed)
                        , rollbackLambda(std::move(that.rollbackLambda)) {
                            that.committed = true;
                        }
                
                        ~ScopeGuard()
                        {
                            if (!committed)
                                rollbackLambda(); // what if this throws?
                        }
                        void commit() { committed = true; } // no need for const
                };
                
                template< typename aLambda , typename rLambda>
                ScopeGuard< rLambda > // return by value is the preferred C++11 way.
                makeScopeGuard( aLambda&& _a , rLambda&& _r) // again perfect forwarding
                {
                    return ScopeGuard< rLambda >( std::forward<aLambda>(_a) , std::forward<rLambda>(_r )); // *** no longer UB, because we're returning by value
                }
                
                template<typename rLambda>
                ScopeGuard< rLambda > makeScopeGuard(rLambda&& _r)
                {
                    return ScopeGuard< rLambda >( std::forward<rLambda>(_r ));
                }
                

                这篇关于最简单最简洁的c++11 ScopeGuard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to print vector#39;s data(如何打印矢量的数据)
                Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                MSVC++ warning flags(MSVC++ 警告标志)
                How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                stack overflow error in C++ program(C++程序中的堆栈溢出错误)

                <tfoot id='TKaIy'></tfoot>

                  <small id='TKaIy'></small><noframes id='TKaIy'>

                    <tbody id='TKaIy'></tbody>
                      <i id='TKaIy'><tr id='TKaIy'><dt id='TKaIy'><q id='TKaIy'><span id='TKaIy'><b id='TKaIy'><form id='TKaIy'><ins id='TKaIy'></ins><ul id='TKaIy'></ul><sub id='TKaIy'></sub></form><legend id='TKaIy'></legend><bdo id='TKaIy'><pre id='TKaIy'><center id='TKaIy'></center></pre></bdo></b><th id='TKaIy'></th></span></q></dt></tr></i><div id='TKaIy'><tfoot id='TKaIy'></tfoot><dl id='TKaIy'><fieldset id='TKaIy'></fieldset></dl></div>

                        <bdo id='TKaIy'></bdo><ul id='TKaIy'></ul>

                        1. <legend id='TKaIy'><style id='TKaIy'><dir id='TKaIy'><q id='TKaIy'></q></dir></style></legend>