Home  >  Article  >  Backend Development  >  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

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
Linda HamiltonOriginal
2024-10-26 10:12:03926browse

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

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

* 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>

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>

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!

Statement:
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