Re: Garbage collection in C++
On 21 Nov., 11:26, James Kanze wrote:
On Nov 20, 2:33 pm, SG wrote:
What you want seems to be a "scope guard".
Yeh. Why be simple when you can be complicated. (Of course,
"lambda", "scope guard", and "cleanup" are just arbitrary names.
Well, the term "scope guard" is just much more popular for this C++
idiom than "cleanup".
But if by "scope guard" you mean the complex mixture of
templates and macros that are needed to achieve this end in
current C++---the cure is worse than the disease.)
No. By scope guard I refer to a common C++ idiom. There's nothing
complex about it. It won't require macros or any compiler specific
hacks. The scope_guard template class doesn't even need any C++0x
features. But what does the implemtation matter? This scope_guard
thingy is just an #include away ...
double* example()
{
double* p = new double[123];
auto && sg = make_scope_guard( [p]{delete[] p;} );
// compute data or throw exception here
sg.cancel(); // makes dtor do nothing
return p;
}
There's no derivation, no virtual functions and no dynamically
allocated function object involved.
But there's library magic required, in order for the scoper
guard to be able to access local variables. If this is possible
as you've written it with the current lambda proposal, fine.
The scope_guard template class implementation doesn't care about what
the function object knows or does. It shouldn't throw an exception
though :) The scope_guard contains this function object that happens
to be a lambda function object in this case. The lambda function
object captured the local pointer p by value. That's where a C++0x
feature comes into play.
If
not, it's basically what I'm asking for, modulo the exact
syntax. Except that I think it's a lot more convenient if I
don't have to invent a name (sg, in your example) for the
variable.
Haven't you noticed that the ability to refer to the scope guard later
on can be very useful? Think of transactions for example.
unique_ptr<double[]> example2()
[...]
It comes with the benefit of the function's declaration being
self- explanatory w.r.t. the life-time management of the
allocated array. It gets even better if you stick with
std::vector which will become movable in C++0x:
std::vector<double> example3();
This works in C++99. I do it all the time.
The difference is that the language standard doesn't force compilers
to do return value optimization as far as I know. Also, it works only
for the first one or two levels of passing that vector around --
assuming RVO + function call that takes its argument by value: func
(example3()).
It seems that the library solution is as convenient and
flexible as a dedicated language feature would be. So, a
"clean up" core language feature is hardly justified.
I've yet to see a library solution which works. But then, I've
I sketched one. I'm 100% certain that the example usage code will be
possible in C++0x.
yet to see much actual code which uses the lambda proposal.
Until we know what "library magic" you were referring to, we
Actually it's not at all magic. I just didn't consider it to be
interesting enough for this thread. I'm confident that you -- as a
professional -- are able to implement it by yourself in no time.
Cheers!
SG