#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! ]
> 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.
> 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.
Sorry, I didn't understand how to use the Boost smart pointers, they
work fine in this case.
> 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