i kind of like it like this but yes every code base should have; i made it like this bcs its more acessible for people to use, it doesnt require them to code it
Different tradeoffs. Mine is super-lightweight and near-instantaneous to compile, but doesn't check for exceptions being thrown and doesn't support dismissal.
File handle should be its own class. For malloc just std::unique_ptr with custom deleter, or handrolled pointer wrapper. If the goal is "not to forget" about calling free by keeping all declarations in one place (the point of acquisition of said pointer) then why remember at all when you have RAII.
I like Alexandrescu's Declarative Flow Control macros which give you SCOPE_EXIT but also SCOPE_FAIL (only executes if the scope exits by throwing an exception) and SCOPE_SUCCESS (only executes if the scope exits NOT by throwing an exception)
He uses the problem of trying to cleanly write a transactional file copy that, if it fails, cleans up properly etc
Pardon my ignorance, but can't you do this with a class that takes one or more lambdas and a reference to a success bool, and simply calls the appropriate functor in the dtor depending if success was reached?
I know it's very slightly non-trivial to implement, but I just don't see why I need to force someone to go look at macros instead of just having it be self contained.
int i = 0;
absl::Cleanup _ = [&] {
std::println("i = {}", i);
};
// ... lots of lines of code with multiple exit paths ...
I'm particularly fond of three parts of this design.
It is locally clear what the lifetime of the captures inside the cleanup is. In this case, it's a default reference capture and so you get reference semantics. This is useful because a lot of these libraries hide that information from the caller so you have to dig into their implementations to know. I'm also not convinced that you can get away with NOT knowing this information in general. The way Go does it, the defer blocks only run on function exit which is very bad for reasoning about nested lifetimes.
It uses CTAD to promote the type of the lambda up to the type of the class template, meaning there's no runtime dispatch here. It's statically known. And as part of that, it uses a deduction guide to signal to tooling "Yes, CTAD is intended here." It also has some trickery to make it harder to spell the final type, to discourage people from using absl::Cleanup as a field in a class or a param of a function. It should only be used as a functional local variable and should not be passed around.
There is no trailing paren after the lambda. It goes directly from curly brace to semicolon. This means when it formats for multi-line purposes, it looks much cleaner imo than some alternatives. Gets it closer to the feeling of a control flow construct.
absl::Cleanup also provides some methods, which is a bit strange but useful at times.
std::move(my_cleanup).Cancel(); will prevent the callback from running.
std::move(my_cleanup).Invoke(); will invoke the callback eagerly and prevent it from running again.
Both of these methods eagerly destroy the callback and they leverage tools like bugprone-use-after-move to ensure you only touch them one time.
Before the release of absl::Cleanup, there was an internal type called gtl::Cleanup<T> that provided similar functionality. However, it also provided a default constructor and an assignment operator to rebind the internal callback. When initialized with std::function<void()> in the type params, you could do some crazy things with it. We pondered making gtl::AnyCleanup to give you this type erased continuation passing functionality, but it was really gross and we decided against that ultimately.
If you link in this library i made u can use "defer" as a unary keyword so u dont need the parenthesis. Tho I've built OpEx w modules so ull probs need to change the cmakelists.txt
Like ud change ur function name to _defer then use defer as the macro keyword
I read: Goto is unacceptable for me. Therefore goto is unacceptable for the rest of you to have too simply because I do not personally have appropriate use cases that others of you may have.
•
u/cpp-ModTeam May 10 '26
It's great that you wrote something in C++ you're proud of! However, please share it in the pinned "Show and Tell" post.
5
u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting May 10 '26
Every codebase should have something like this. Here's my implementation: https://github.com/vittorioromeo/VRSFML/blob/master/include/SFML/Base/ScopeGuard.hpp
I personally like having the user provide the curly braces as part of the macro invocation.
12
u/brickman1444 May 10 '26
What's the advantage over using a class with a destructor?
6
u/schmerg-uk May 10 '26
I like Alexandrescu's Declarative Flow Control macros which give you SCOPE_EXIT but also SCOPE_FAIL (only executes if the scope exits by throwing an exception) and SCOPE_SUCCESS (only executes if the scope exits NOT by throwing an exception)
He uses the problem of trying to cleanly write a transactional file copy that, if it fails, cleans up properly etc
https://www.youtube.com/watch?v=WjTrfoiB0MQ
https://github.com/CppCon/CppCon2015/blob/master/Presentations/Declarative%20Control%20Flow/Declarative%20Control%20Flow%20-%20Andrei%20Alexandrescu%20-%20CppCon%202015.pdf
Where SCOPE_... are macros
3
u/Natural_Builder_3170 May 10 '26
Since you’re using FILE* can’t you manage it with a unique pointer and set the deleter to fclose?
3
u/javascript May 10 '26
I created
absl::Cleanupwhich serves a similar purpose. :)You can check it out here: https://github.com/abseil/abseil-cpp/blob/master/absl/cleanup/cleanup.h
No macros! Yay!
In particular, the code example at the top of the file is similar to the code example in this reddit post: https://github.com/abseil/abseil-cpp/blob/30bba84041ba0aadd2c31b52742b8157db047a2f/absl/cleanup/cleanup.h#L29-L56
For a shorter example, you can just do this:
I'm particularly fond of three parts of this design.
absl::Cleanupas a field in a class or a param of a function. It should only be used as a functional local variable and should not be passed around.absl::Cleanupalso provides some methods, which is a bit strange but useful at times.std::move(my_cleanup).Cancel();will prevent the callback from running.std::move(my_cleanup).Invoke();will invoke the callback eagerly and prevent it from running again.Both of these methods eagerly destroy the callback and they leverage tools like
bugprone-use-after-moveto ensure you only touch them one time.Before the release of
absl::Cleanup, there was an internal type calledgtl::Cleanup<T>that provided similar functionality. However, it also provided a default constructor and an assignment operator to rebind the internal callback. When initialized withstd::function<void()>in the type params, you could do some crazy things with it. We pondered makinggtl::AnyCleanupto give you this type erased continuation passing functionality, but it was really gross and we decided against that ultimately.2
u/Electronic_Tap_8052 May 10 '26
Is this any different from a generic class that takes a lambda and executes it in the destructor?
2
u/SamG101_ May 10 '26
https://github.com/SamG101-Developer/OpEx
If you link in this library i made u can use "defer" as a unary keyword so u dont need the parenthesis. Tho I've built OpEx w modules so ull probs need to change the cmakelists.txt
Like ud change ur function name to _defer then use defer as the macro keyword
1
u/jeezfrk MT Linux/Telco May 10 '26
BOOST_SCOPE_EXIT
1
u/eXl5eQ May 10 '26
I have my
util::defer _([&]() { close(something); });But why calling it "Zig-like"? Doesn't Go introduced this feature much earlier than Zig?
0
u/TheRealSmolt May 10 '26
Overall, pretty good template/safety code quality. Nice 👍
12
u/[deleted] May 10 '26 edited May 10 '26
[removed] — view removed comment