Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

boost smart pointer with exception in ctor

0 views
Skip to first unread message

John

unread,
Mar 18, 2007, 4:33:23 PM3/18/07
to
I need a very simple smart pointer that deals with exceptions during
construction. The following demonstrates:

#include <iostream>
#include <exception>

struct A {
A() { throw std::exception("throw from A"); }
};

class B : private A {
};

int main()
{
B* C_ = 0;
try {
C_ = new B;
} catch(const std::exception& e) {
std::cout << e.what();
}
delete C_; // C_ is 0
return 0;
}

As I understand it, calling delete on a pointer == 0 is undefined,
therefore I need a smart pointer to resolve this. I would like to use a
standard library for this. I must be missing something because as far as
I can tell, the boost library smart pointers don't handle this simple
scenario.
Thanks.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Martin Vuille

unread,
Mar 18, 2007, 6:56:07 PM3/18/07
to
John <n...@spam.com> wrote in news:HY6Lh.122944$cE3.47356@edtnps89:

> As I understand it, calling delete on a pointer == 0 is
> undefined,

This is what the C++ standard has to say about that (5.3.5/2):

"[...] if the value of the operand of delete is the null pointer the
operation has no effect."

MV

--
I do not want replies; please follow-up to the group.

Thomas Maeder

unread,
Mar 18, 2007, 7:00:00 PM3/18/07
to
John <n...@spam.com> writes:

> As I understand it, calling delete on a pointer == 0 is undefined,

Your understanding is wrong.

Calling delete on a pointer whose value is equal to the null pointer
constant is a defined operation; the effect is that nothing observable
is done.

John

unread,
Mar 18, 2007, 6:56:08 PM3/18/07
to
John wrote:
> I need a very simple smart pointer that deals with exceptions during
> construction.

Sorry, I didn't understand how to use the Boost smart pointers, they
work fine in this case.

James Kanze

unread,
Mar 18, 2007, 6:56:09 PM3/18/07
to
On Mar 18, 9:33 pm, John <n...@spam.com> wrote:

> I need a very simple smart pointer that deals with exceptions during
> construction.

Most of the smart pointers I've used can't raise an exception
during construction. I think that those that do (e.g.
boost::shared_ptr) handle it correctly.

> The following demonstrates:

> #include <iostream>
> #include <exception>

> struct A {
> A() { throw std::exception("throw from A"); }
> };

> class B : private A {
> };

> int main()
> {
> B* C_ = 0;
> try {
> C_ = new B;
> } catch(const std::exception& e) {
> std::cout << e.what();
> }
> delete C_; // C_ is 0
> return 0;
> }

Wouldn't it be more natural to write:

try {
B* c = new B ;
// do whatever with c...
} catch ( std::exceptioin& e ) {
std::cout << e.what() << std::endl ;
}

The whole point of an exception in an initializer is that the
object doesn't exist afterwards, so you don't have to worry
about what state it is in.

> As I understand it, calling delete on a pointer == 0 is undefined,

You understand wrong. Deleting a null pointer is perfectly
legal and acceptable practice.

> therefore I need a smart pointer to resolve this. I would like to use a
> standard library for this. I must be missing something because as far as
> I can tell, the boost library smart pointers don't handle this simple
> scenario.

The above code, except for the delete, should work perfectly
with boost::shared_ptr, std::auto_ptr, or just about any smart
pointer worth using. Still, I would consider putting the
definition of the pointer in the try block.

--
James Kanze (Gabi Software) email: james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

0 new messages