This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
Re: Will therefore GDB utilize C++ or not?
- From: Jan Kratochvil <jan dot kratochvil at redhat dot com>
- To: Pedro Alves <palves at redhat dot com>
- Cc: Tom Tromey <tromey at redhat dot com>, gdb at sourceware dot org
- Date: Wed, 21 Nov 2012 21:18:05 +0100
- Subject: Re: Will therefore GDB utilize C++ or not?
- References: <20120330161403.GA17891@host2.jankratochvil.net> <87aa2rjkb8.fsf@fleche.redhat.com> <4F832D5B.9030308@redhat.com> <20120409190519.GA524@host2.jankratochvil.net> <4F833D29.4050102@redhat.com> <8762cwpz3u.fsf@fleche.redhat.com> <4FBA6D04.7060804@redhat.com>
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