testing if shared_ptr has been assigned
I need some help in testing for the condition of whether a shared_ptr has been assigned. In the following example, the "if( b != NULL ) does not work. How would I do this test? typedef boost::shared_ptr<int> IPtr; int i( 0 ); IPtr a( i ); IPtr b; if( some condition ) b = a; if( b != NULL ) { // How should this really be coded? } Best Regards, Steve [Non-text portions of this message have been removed]
On Wednesday, December 11, 2002, at 11:01 AM, TEA Hartmann, Steven wrote:
I need some help in testing for the condition of whether a shared_ptr has been assigned. In the following example, the "if( b != NULL ) does not work. How would I do this test?
typedef boost::shared_ptr<int> IPtr;
int i( 0 ); IPtr a( i ); IPtr b;
if( some condition ) b = a;
if( b != NULL ) { // How should this really be coded? }
if (b) -- Darin
----- Original Message -----
From: "Darin Adler"
if (b)
-- Darin
Personally, I use if ( b.get() ), because on at least one compiler (Sun Workshop 5), the simpler syntax caused a compiler error.
if (b)
-- Darin
Personally, I use if ( b.get() ), because on at least one compiler (Sun Workshop 5), the simpler syntax caused a compiler error.
I prefer to use if(!!b) because then it will still work if you change the pointer type later on in the project and only requires an operator!() instead of the more dangerous operator bool (compiler can do automatic type conversions behind your back leading to possible bugs if you are not careful). - Steven Green http://www.greenius.ltd.uk
On Monday 16 December 2002 07:05 am, Steven Green wrote:
if (b)
-- Darin
Personally, I use if ( b.get() ), because on at least one compiler (Sun Workshop 5), the simpler syntax caused a compiler error.
I prefer to use if(!!b) because then it will still work if you change the pointer type later on in the project and only requires an operator!() instead of the more dangerous operator bool (compiler can do automatic type conversions behind your back leading to possible bugs if you are not careful).
- Steven Green http://www.greenius.ltd.uk
FWIW, shared_ptr doesn't actually have an operator bool. It uses the safe_bool idiom (as do other Boost libraries) to eliminate most automatic type conversions. Doug
participants (5)
-
Andrew R. Thomas-Cramer
-
Darin Adler
-
Douglas Gregor
-
Steven Green
-
TEA Hartmann, Steven