Home > Backend Development > C++ > body text

The following are several English titles that match the content of the article: Concise title (short and direct): * How to Build a Simple ScopeGuard with C 11 Idioms? * Implementing a Simple ScopeGuard in C 11: A Practical Approach * C 11 ScopeGuard: A Concise and Practical I

Linda Hamilton
Release: 2024-10-26 10:12:03
Original
925 people have browsed it

以下是几个符合文章内容的英文标题:

简洁的标题(简短且直接):

* How to Build a Simple ScopeGuard with C  11 Idioms? 
* Implementing a Simple ScopeGuard in C  11: A Practical Approach
* C  11 ScopeGuard: A Concise and Practical Implementation

更详细的标题(阐明文章重点):

* C  11 ScopeGuard: A S

Concise and practical ScopeGuard in C 11

Question:

How to use C 11 idioms to write based on Alexandrescu concept Simple ScopeGuard?

Answer:

The following is a ScopeGuard implementation based on C 11 idiom:

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

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

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

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

    template< typename aLambda , typename rLambda>
    const ScopeGuard< rLambda >&amp; makeScopeGuard( const aLambda&amp; _a , const rLambda&amp; _r)
    {
        return ScopeGuard< rLambda >( _a , _r );
    }

    template<typename rLambda>
    const ScopeGuard< rLambda >&amp; makeScopeGuard(const rLambda&amp; _r)
    {
        return ScopeGuard< rLambda >(_r );
    }
}</code>
Copy after login

Usage:

<code class="cpp">void SomeFuncThatShouldBehaveAtomicallyInCaseOfExceptions() 
{
   std::vector<int> myVec;
   std::vector<int> someOtherVec;

   myVec.push_back(5);
   //first constructor, adquire happens elsewhere
   const auto&amp; a = RAII::makeScopeGuard( [&amp;]() { myVec.pop_back(); } );  

   //sintactically neater, since everything happens in a single line
   const auto&amp; b = RAII::makeScopeGuard( [&amp;]() { someOtherVec.push_back(42); }
                     , [&amp;]() { someOtherVec.pop_back(); } ); 

   b.commit();
   a.commit();
}</code>
Copy after login

Advantages:

  • Simple and easy to use
  • Concise code
  • Supports exception handling

Potential issues:

  • If the advise lambda function throws an exception, the release lambda function is never called because it never fully constructs the ScopeGuard.
  • If the cleanup function throws an exception, the program will terminate unexpectedly.

The above is the detailed content of The following are several English titles that match the content of the article: Concise title (short and direct): * How to Build a Simple ScopeGuard with C 11 Idioms? * Implementing a Simple ScopeGuard in C 11: A Practical Approach * C 11 ScopeGuard: A Concise and Practical I. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!