Re: Will therefore GDB utilize C++ or not?

Jan Kratochvil
2012-11-21T20:18:18Z

On Mon, 21 May 2012 18:27:48 +0200, Pedro Alves wrote:
> What I meant by not that different, is, that with RAII you get
> to write something like (the dumbest version of a RAII object
> that supports canceling possible):
>
> class my_raii_thing
> {
> private:
> whatever_arg *arg; << moral equivalent of the cleanup args.
> bool discarded;
>
> public:
> my_raii_thing (whatever_arg *p) : arg (p), discarded(false)
> {}
>
> ~my_raii_thing () << moral equivalent of a cleanup function.
> {
> if (!discarded)
> {
> // whatever to release this->arg or something like that.
> }
> }
>
> void discard ()
> {
> discarded = true;
> }
> };
>
> and then do:
>
> my_raii_thing foo (&whatever_arg);
>
> if (whatnot)
> {
> whatever_arg.discard();
> return SUCESS;
> }
> }

I find it too complicated, why not just shared_ptr:

#include <memory>
#include <iostream>
using namespace std;
class R {
public:
R() { cout << "R new" << endl; }
~R() { cout << "R delete" << endl; }
string val() { return "isR"; }
};
static shared_ptr<R> getR(bool fail) {
auto v=shared_ptr<R>(new R());
if (fail)
throw fail;
return v;
}
int main() {
try {
getR(true);
} catch (...) {}
cout << getR(false)->val() << endl;
}

R new
R delete
R new
isR
R delete

As C++11 dependency is probably too strict one can use Boost instead and as GDB
does not like external dependencies stripped down Boost can be bundled to the
sourceware repository - on normal systems sure C++11 libraries get used instead.

Jan