Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::scope_fail

      From cppreference.com
      <cpp‎ |experimental

      [edit template]
       
       
       
       
       
      Defined in header<experimental/scope>
      template<class EF>
      class scope_fail;
      (library fundamentals TS v3)

      The class templatescope_fail is a general-purpose scope guard intended to call its exit function when a scope is exited via an exception.

      scope_fail is notCopyConstructible,CopyAssignable orMoveAssignable, however, it may beMoveConstructible ifEF meets some requirements, which permits wrapping ascope_fail into another object.

      Ascope_fail may be either active, i.e. calls its exit function on destruction, or inactive, i.e. does nothing on destruction. Ascope_fail is active after constructed from an exit function.

      Ascope_fail can become inactive by callingrelease() on it either manually or automatically (by the move constructor). An inactivescope_fail may also be obtained by initializing with another inactivescope_fail. Once ascope_fail is inactive, it cannot become active again.

      Ascope_fail effectively holds anEF and abool flag indicating if it is active, alongwith a counter of uncaught exceptions used for detecting whether the destructor is called during stack unwinding.

      Contents

      [edit]Template parameters

      EF - type of stored exit function
      Type requirements
      -
      EF shall be either:
      -
      Calling an lvalue ofstd::remove_reference_t<EF> with no argument shall be well-formed.

      [edit]Member functions

      constructs a newscope_fail
      (public member function)[edit]
      calls the exit function when the scope is exited via an exception if thescope_fail is active, then destroys thescope_fail
      (public member function)[edit]
      operator=
      [deleted]
      scope_fail is not assignable
      (public member function)
      Modifiers
      makes thescope_fail inactive
      (public member function)[edit]

      [edit]Deduction guides

      [edit]Notes

      Constructing ascope_fail of dynamic storage duration might lead to unexpected behavior.

      Constructing ascope_fail is constructed from anotherscope_fail created in a different thread might also lead to unexpected behavior since the count of uncaught exceptions obtained in different threads may be compared during the destruction.

      [edit]Example

      Run this code
      #include <iostream>#include <cstdlib>#include <string_view>#include <experimental/scope> void print_exit_status(std::string_view name,bool exit_status,bool did_throw){std::cout<< name<<":\n";std::cout<<"  Throwed exception  "<<(did_throw?"yes":"no")<<"\n";std::cout<<"  Exit status        "<<(exit_status?"finished":"pending")<<"\n\n";} // Randomly throw an exception (50% chance)void maybe_throw(){if(std::rand()>=RAND_MAX/2)throwstd::exception{};} int main(){bool exit_status{false}, did_throw{false}; // Manual handling at "end of scope"try{    maybe_throw();    exit_status=true;}catch(...){ did_throw=true;}  print_exit_status("Manual handling", exit_status, did_throw); // Using scope_exit: runs on scope exit (success or exception)  exit_status= did_throw=false;try{auto guard= std::experimental::scope_exit{[&]{ exit_status=true;}};    maybe_throw();}catch(...){ did_throw=true;}  print_exit_status("scope_exit", exit_status, did_throw); // Using scope_fail: runs only if an exception occurs  exit_status= did_throw=false;try{auto guard= std::experimental::scope_fail{[&]{ exit_status=true;}};    maybe_throw();}catch(...){ did_throw=true;}  print_exit_status("scope_fail", exit_status, did_throw); // Using scope_success: runs only if no exception occurs  exit_status= did_throw=false;try{auto guard= std::experimental::scope_success{[&]{ exit_status=true;}};    maybe_throw();}catch(...){ did_throw=true;}  print_exit_status("scope_success", exit_status, did_throw);}

      Output:

      Manual handling:  Throwed exception  yes  Exit status        pending scope_exit:  Throwed exception  no  Exit status        finished scope_fail:  Throwed exception  yes  Exit status        finished scope_success:  Throwed exception  yes  Exit status        pending

      [edit]See also

      wraps a function object and invokes it on exiting the scope
      (class template)[edit]
      wraps a function object and invokes it on exiting the scope normally
      (class template)[edit]
      default deleter forunique_ptr
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/experimental/scope_fail&oldid=131206"

      [8]ページ先頭

      ©2009-2025 Movatter.jp