Re: general purpose RAII with closures
On 12 sep, 12:10, JoshuaMaur...@gmail.com wrote:
Am I correct in concluding that the only way to get what I want is
decl_type and/or auto in C++0x? I suddenly like auto that much more. I
was just wondering if there was some hackery, possibly with macros, to
get what I want in C++98. I believe not. (Now, if I was willing to
accept a virtual function call and new and delete some objects for
each instance of call_on_stack_unwind_t, I could pull it off. That's
excessive, and wasteful of run time speed IMHO.)
You would only need to make the destructor virtual.
struct call_on_stack_base
{
virtual ~call_on_stack_base() = 0;
};
template<typename T>
struct call_on_stack_unwind_t : call_on_stack_base
{
call_on_stack_unwind_t(T t) : f(t) {}
~call_on_stack_unwind_t() { f(); }
T f;
};
template<typename T>
call_on_stack_unwind_t<T>
call_on_stack_unwind(T t)
{
return t;
}
int main()
{
Handle* h = allocate();
const call_on_stack_base& helper =
call_on_stack_unwind(bind(deallocate, h));
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]