std::experi...ope_success(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 πŸŽ€

  • EF shall be either:
    • a Destructible FunctionObject type,
    • an lvalue reference to FunctionObject,
    • an lvalue reference to function.
  • 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_success is active, then destroys the scope_success (public member function) πŸ’€
  • operator= β€” scope_success is not assignable [deleted] (public member function) πŸ™ˆ

⭐ Modifiers ⭐

  • release β€” makes the scope_success inactive (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_success of dynamic storage duration might lead to unexpected behavior 😬
  • Constructing a scope_success from another scope_success 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. threading said "not today" 😭
  • If the EF stored in a scope_success object 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 the scope_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 for unique_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