#include <memory>
#include <iostream>
int main() {
std::shared_ptr<void> at_exit_1(nullptr, [](...){ std::cout << 4; });
std::cout << 1;
std::shared_ptr<void> at_exit_2(nullptr, [](...){ std::cout << 3; });
std::cout << 2;
}
which could be rewritten like:#include <memory>
#include <iostream>
int main() {
defer std::shared_ptr<void>(nullptr, [](...){ std::cout << 4; });
std::cout << 1;
defer std::shared_ptr<void>(nullptr, [](...){ std::cout << 3; });
std::cout << 2;
}
without having to care for naming without necessity.
I know such kind of situation happen not only when using that kind of at_exit
trick, but also, this is a feature that couples perfectly to std::lock_guard
or any kind of guard for that matter.
This would also have a strong connection with the scope_exit & unique_resource
proposal.
The keyword is inspired in the defer keyword from the Go programming language.
Actually, I just happened to come here after reviewing some questions on StackOverflow
where I have had participation:
- http://stackoverflow.com/questions/33050620
Regards,
Francisco Lopes
| From: Francisco Lopes Sent: Saturday, July 9, 2016 6:26 PM To: ISO C++ Standard - Future Proposals Reply To: std-pr...@isocpp.org Subject: [std-proposals] Re: deferring destruction |
It has been some years this idea floats in my mind as a nice feature, so I'd like
to ask you guys whether you also feel it would be helpful as I think so, it's my first proposal sketch here.
I'd like to suggest a new keyword, defer, whose main purpose is to extend
the lifetime of a temporary following it, to the enclosing scope.
The keyword is inspired in the defer keyword from the Go programming language.
Actually, I just happened to come here after reviewing some questions on StackOverflow
where I have had participation:
- http://stackoverflow.com/questions/33050620