This is one of my favorite features of unique_ptr. It is an all-purpose way to make something happen at the end of scope without having to write a new struct and destructor. You don't even need the pointer at all since you can simply do:
That is a pretty cool use. In practice though, it is pretty unreadable unless you wrap it in a macro or template, and if you are doing that you might as well just implement it yourself. Something like:
that's the beauty of using destructors to clean things when an object goes out of scope.
in your OnScopeExit we could use templates (and not use std::function), that way any callable thing could be called - and it would be lighter than std::function
I actually don't think there's a nice way of doing this without making the interface slightly more annoying.
If you make OnScopeExit a template on a callable, then you need to give it the type of the callable explicitly on construction, which means that using a lambda becomes more annoying. I.e., you'd have to do it like this:
auto f = []() { std::cout << "Exited Scope!" << std::endl; };
OnScopeExit<decltype(f)> x(f);
The way unique_ptr does it is by using type erasure, which requires a virtual function call internally, just like std::function.
With all due respect to Alexandrescu's incredible C++ skills, I find that his style of programming is very detrimental to the overall quality of the C++ environment.
C++ is a very abusable language. This is reflected both in its capabilities and in the ability to shoot yourself in the foot. These points are obviously well-covered in any C++ discussion.
Now, Alexandrescu brings a style of programming in which you exploit templates, sometimes macros, operator overloading, and everything else, in order to squeeze out more generality/reusability out of a piece of code, or simplify the coder-visible syntax of some construct.
We see that right here: the above comments show a few ways to run arbitrary code on a scope exit. Each of the examples above is perfectly easy to follow along. But then the YouTube video show's Alexandrescu's take, and it's a real brain-warper. He invokes a couple of levels of C++ trickery to get his effect. Now, his end result is wonderful... but it's also not easily understandable. If you write code in his style at your job, good luck to the person that takes over your code after you leave the project. They'll have a hell of a time.
IMHO a developer should always sacrifice syntactic sugar, with no performance penalty if done correctly, in exchange for more understandable code.
I understand that your point is that unique_ptr facilitates resource management. But the example you provide is a misleading. How is this supposed to compile? The deleter type is part of the unique_ptr. So you need something along these lines:
auto scope_guard = [](void*) { };
unique_ptr<void, decltype(scope_guard)> finally{nullptr, scope_guard};
This is not very idiomatic. Something like toth mentioned makes more sense. If you want it to be efficient, use this:
minor nitpicks: please use the constructor's initializer list for initialization of class members [1] and do not use hungarian notation or forms thereof with other names which I cannot remember [2]
19 comments
[ 4.2 ms ] story [ 23.0 ms ] threadin your OnScopeExit we could use templates (and not use std::function), that way any callable thing could be called - and it would be lighter than std::function
http://youtu.be/WjTrfoiB0MQ
C++ is a very abusable language. This is reflected both in its capabilities and in the ability to shoot yourself in the foot. These points are obviously well-covered in any C++ discussion.
Now, Alexandrescu brings a style of programming in which you exploit templates, sometimes macros, operator overloading, and everything else, in order to squeeze out more generality/reusability out of a piece of code, or simplify the coder-visible syntax of some construct.
We see that right here: the above comments show a few ways to run arbitrary code on a scope exit. Each of the examples above is perfectly easy to follow along. But then the YouTube video show's Alexandrescu's take, and it's a real brain-warper. He invokes a couple of levels of C++ trickery to get his effect. Now, his end result is wonderful... but it's also not easily understandable. If you write code in his style at your job, good luck to the person that takes over your code after you leave the project. They'll have a hell of a time.
IMHO a developer should always sacrifice syntactic sugar, with no performance penalty if done correctly, in exchange for more understandable code.
That's shared_ptr. unique_ptr does not perform type erasure.
It provides some macros to make it easier to use, e.g.
[1] http://stackoverflow.com/questions/15679977/constructor-init...
[2] http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#...