std::experi...ope_success(3)
π std::experimental::scope_success π
okay bestie, so we're talking about std::experimental::scope_success from the C++ Standard Library β¨ it's like, a reeeeally specific little tool and honestly? kinda iconic once you get what it does π
β¨ NAME β¨
std::experimental::scope_success β yeah that's the whole name, no cap π
πΈ Synopsis πΈ
so this bad boy lives in the header <experimental/scope> and here's the whole vibe:
template< class EF > (library fundamentals TS v3)
class scope_success;
the class template scope_success is a general-purpose scope guard whose entire main character purpose is to call its exit function when a scope is normally exited. periodt π
now listen up: scope_success is not CopyConstructible, not CopyAssignable, and not MoveAssignable β like, it does NOT do copies of itself bestie π
ββοΈ but! it may be MoveConstructible if EF meets some requirements, which is what lets you wrap a scope_success into another object. slay π¦
a scope_success can be either:
- active β meaning it calls its exit function on destruction π₯
- inactive β meaning it literally does nothing on destruction π
a scope_success is active after it's constructed from an exit function. simple as that β¨
you can make it inactive by calling release() on it, either manually or automatically (the move constructor does it for you, cute π). you can ALSO get an inactive one by initializing with another inactive scope_success. but here's the tea: once a scope_success is inactive, it cannot become active again. no take-backs, no revivals, it's over π
under the hood a scope_success effectively holds an EF and a <yellow>bool</yellow> flag saying if it's active, along with a counter of uncaught exceptions β that counter is how it detects whether the destructor is being called during stack unwinding. iykyk π§
π¦ Template parameters π¦
EFβ type of stored exit function π
π Type requirements π
EFshall be either:- a
DestructibleFunctionObjecttype, - an lvalue reference to
FunctionObject, - an lvalue reference to function.
- a
- Calling an lvalue of
std::remove_reference_t<EF>with no argument shall be well-formed. no notes, just facts π
π₯ Member functions π₯
- constructor β constructs a new
scope_success(public member function) π - destructor β calls the exit function when the scope is exited normally if the
scope_successis active, then destroys thescope_success(public member function) π operator=βscope_successis not assignable [deleted] (public member function) π
β Modifiers β
releaseβ makes thescope_successinactive (public member function) π«Ά
Deduction guides
(yes bestie, there are deduction guides in the mix β¨)
π Notes π
okay these are the warnings, pay attention fr fr π
- Constructing a
scope_successof dynamic storage duration might lead to unexpected behavior π¬ - Constructing a
scope_successfrom anotherscope_successcreated 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. threading said "not today" π - If the
EFstored in ascope_successobject refers to a local variable of the function where it's defined β like, say, a lambda capturing the variable by reference β and that variable is used as a return operand in that function, then that variable might have already been returned when thescope_success's destructor executes and calls the exit function. which can lead to surprising behavior π consider yourself warned bestie
π Example π
This section is incomplete Reason: no example
(yeah, they really left us hanging on this one π no example for us, tragic)
πΈ See also πΈ
scope_exitβ wraps a function object and invokes it on exiting the scope (class template) π¦scope_failβ wraps a function object and invokes it on exiting the scope through an exception (class template) π₯default_deleteβ default deleter forunique_ptr(C++11) (class template) π
Category:
- Todo no example π
okay that's the whole page bestie, you're officially a scope_success expert now, go forth and guard those scopes ππ
source: cppreference.com, dated 2024.06.10 β mwah, love u, bye π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms