473,779 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

boost::shared_p tr - NULL

If a method is declared to return a type boost::shared_p tr<sometype>,
how can the method be changed to do the equivalent of returning NULL
when it was declared to return a raw pointer?

Dec 1 '07 #1
9 9890
On Sat, 1 Dec 2007 11:36:51 -0800 (PST) in comp.lang.c++,
Christopher <cp***@austin.r r.comwrote,
>Are you referring to the boost manual?
http://www.boost.org/libs/smart_ptr/scoped_ptr.htm

I did and all I see is a reset function which sets the internal
pointer to NULL, but it doesn't say how to check if it is NULL,
How about
if (retvalue == boost::shared_p tr<sometype>()) {

Think!
>and it
says that behavior is undefined if you dereference it while the
internal pointer is NULL.
Yes, don't do that.

Dec 1 '07 #2
On Dec 1, 9:19 pm, David Harmon <sou...@netcom. comwrote:
On Sat, 1 Dec 2007 11:36:51 -0800 (PST) in comp.lang.c++,
Christopher <cp...@austin.r r.comwrote,
Are you referring to the boost manual?
http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
I did and all I see is a reset function which sets the internal
pointer to NULL, but it doesn't say how to check if it is NULL,
How about
if (retvalue == boost::shared_p tr<sometype>()) {
How about:
if ( retvalue == NULL ) { ... }

I've not looked at boost::shared_p tr in this regard, but all of
the intelligent pointers I've written accept it.

In practice, if I think there's the slightest chance of using
intelligent pointers, I'll write something like:

template< typename T >
bool
isValid( T const* p )
{
return p != NULL ;
}

and use it. Adding the necessary overloads for the intelligent
pointers as needed. (This allows easily switching back, when
you realize that the smart pointer doesn't work.) But as I
said, a correctly designed intelligent pointer should support
all of the idioms a raw pointer does: "if( ptr == NULL )", and
even "if ( ptr )" (although I've never seen a coding guideline
that would allow use of the latter).

And if worse comes to worse, there's always:
if ( ptr.get() == NULL ) ...

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 2 '07 #3
On Dec 1, 12:19 pm, David Harmon <sou...@netcom. comwrote:
On Sat, 1 Dec 2007 11:36:51 -0800 (PST) in comp.lang.c++,
Christopher <cp...@austin.r r.comwrote,
Are you referring to the boost manual?
http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
I did and all I see is a reset function which sets the internal
pointer to NULL, but it doesn't say how to check if it is NULL,

How about
if (retvalue == boost::shared_p tr<sometype>()) {
How about the more succinct:

if (not retval) {

or, alternately:

if (retval == false) {

Greg
Dec 3 '07 #4
James Kanze a écrit :
On Dec 1, 9:19 pm, David Harmon <sou...@netcom. comwrote:
>On Sat, 1 Dec 2007 11:36:51 -0800 (PST) in comp.lang.c++,
Christopher <cp...@austin.r r.comwrote,
>>Are you referring to the boost manual?
http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
>>I did and all I see is a reset function which sets the internal
pointer to NULL, but it doesn't say how to check if it is NULL,
>How about
if (retvalue == boost::shared_p tr<sometype>()) {

How about:
if ( retvalue == NULL ) { ... }

I've not looked at boost::shared_p tr in this regard, but all of
the intelligent pointers I've written accept it.
Boost share_ptr doesn't provide a operator==(smar t_ptr<T>& smart,T*ptr).

I could not find the exact reason in the original proposal but I guess
it is an efficiency matter: it would be equivalent to
'(smart.use_cou nt()?smart.ptr: NULL) == ptr' when the intended operations
in the case ptr=NULL would have been '(use_count()== 0) ||
(smart.ptr==NUL L)'.
>
In practice, if I think there's the slightest chance of using
intelligent pointers, I'll write something like:

template< typename T >
bool
isValid( T const* p )
{
return p != NULL ;
}

and use it. Adding the necessary overloads for the intelligent
pointers as needed. (This allows easily switching back, when
you realize that the smart pointer doesn't work.) But as I
said, a correctly designed intelligent pointer should support
all of the idioms a raw pointer does: "if( ptr == NULL )", and
even "if ( ptr )" (although I've never seen a coding guideline
that would allow use of the latter).

And if worse comes to worse, there's always:
if ( ptr.get() == NULL ) ...
But smart-ptr does provide conversion to unspecified-bool-type.

The doc is quite explicit:

*conversions*

operator unspecified-bool-type () const; // never throws

Returns: an unspecified value that, when used in boolean contexts,
is equivalent to get() != 0.

Throws: nothing.

Notes: This conversion operator allows shared_ptr objects to be
used in boolean contexts, like if (p && p->valid()) {}. The actual
target type is typically a pointer to a member function, avoiding many
of the implicit conversion pitfalls.

[The conversion to bool is not merely syntactic sugar. It allows
shared_ptrs to be declared in conditions when using dynamic_pointer _cast
or weak_ptr::lock.]

Michael
Dec 3 '07 #5
On Dec 3, 6:36 am, Michael DOUBEZ <michael.dou... @free.frwrote:
James Kanze a écrit :
On Dec 1, 9:19 pm, David Harmon <sou...@netcom. comwrote:
On Sat, 1 Dec 2007 11:36:51 -0800 (PST) in comp.lang.c++,
Christopher <cp...@austin.r r.comwrote,
>Are you referring to the boost manual?
http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
>I did and all I see is a reset function which sets the internal
pointer to NULL, but it doesn't say how to check if it is NULL,
How about
if (retvalue == boost::shared_p tr<sometype>()) {
How about:
if ( retvalue == NULL ) { ... }
I've not looked at boost::shared_p tr in this regard, but all of
the intelligent pointers I've written accept it.
Boost share_ptr doesn't provide a operator==(smar t_ptr<T>& smart,T*ptr).
I'm not sure that that's what is wanted. In my own smart
pointers, I use something like:

template< typename T >
class SmartPtr
{
struct Hidden {} ;
public:
// ...
friend operator==( SmartPtr< T const&, Hidden const* ) ;
friend operator!=( Hidden const*, SmartPtr< T const& ) ;
// ...
} ;

Null pointer constants convert to Hidden const*, but the user
can't get one any other way.
I could not find the exact reason in the original proposal but I guess
it is an efficiency matter: it would be equivalent to
'(smart.use_cou nt()?smart.ptr: NULL) == ptr' when the intended operations
in the case ptr=NULL would have been '(use_count()== 0) ||
(smart.ptr==NUL L)'.
I can't really believe that the difference in efficiency would
make a difference here.
In practice, if I think there's the slightest chance of using
intelligent pointers, I'll write something like:
template< typename T >
bool
isValid( T const* p )
{
return p != NULL ;
}
and use it. Adding the necessary overloads for the intelligent
pointers as needed. (This allows easily switching back, when
you realize that the smart pointer doesn't work.) But as I
said, a correctly designed intelligent pointer should support
all of the idioms a raw pointer does: "if( ptr == NULL )", and
even "if ( ptr )" (although I've never seen a coding guideline
that would allow use of the latter).
And if worse comes to worse, there's always:
if ( ptr.get() == NULL ) ...
But smart-ptr does provide conversion to unspecified-bool-type.
Yes. But this forces you do use something like:

if( ptr ) ...

Gratuous obfuscation, and forbidden by all of the coding
guidelines I've seen. (Never the less, I'd support it as well.
Because raw pointers do.)

Of course, from a QoI point of view, you wouldn't use bool, but
"Hidden const*", to avoid any chance of mis-use.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 3 '07 #6
James Kanze a écrit :
On Dec 3, 6:36 am, Michael DOUBEZ <michael.dou... @free.frwrote:
>James Kanze a écrit :
>>On Dec 1, 9:19 pm, David Harmon <sou...@netcom. comwrote:
On Sat, 1 Dec 2007 11:36:51 -0800 (PST) in comp.lang.c++,
Christophe r <cp...@austin.r r.comwrote,
Are you referring to the boost manual?
http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
I did and all I see is a reset function which sets the internal
pointer to NULL, but it doesn't say how to check if it is NULL,
How about
if (retvalue == boost::shared_p tr<sometype>()) {
How about:
if ( retvalue == NULL ) { ... }
I've not looked at boost::shared_p tr in this regard, but all of
the intelligent pointers I've written accept it.
>Boost share_ptr doesn't provide a operator==(smar t_ptr<T>& smart,T*ptr).

I'm not sure that that's what is wanted. In my own smart
pointers, I use something like:

template< typename T >
class SmartPtr
{
struct Hidden {} ;
public:
// ...
friend operator==( SmartPtr< T const&, Hidden const* ) ;
friend operator!=( Hidden const*, SmartPtr< T const& ) ;
// ...
} ;

Null pointer constants convert to Hidden const*, but the user
can't get one any other way.
Yes. Unless T is void but I guess it doesn't happen often.
Michael
Dec 3 '07 #7
On Dec 3, 2:39 pm, James Kanze <james.ka...@gm ail.comwrote:
On Dec 3, 6:36 am, Michael DOUBEZ <michael.dou... @free.frwrote:
But smart-ptr does provide conversion to unspecified-bool-type.

Yes. But this forces you do use something like:

if( ptr ) ...

Gratuous obfuscation, and forbidden by all of the coding
guidelines I've seen. (Never the less, I'd support it as well.
Because raw pointers do.)

Of course, from a QoI point of view, you wouldn't use bool, but
"Hidden const*", to avoid any chance of mis-use.
Why is it a forbidden guideline? What possible mis-use can this be?
Dec 3 '07 #8
Abhishek Padmanabh a écrit :
On Dec 3, 2:39 pm, James Kanze <james.ka...@gm ail.comwrote:
>On Dec 3, 6:36 am, Michael DOUBEZ <michael.dou... @free.frwrote:
>>But smart-ptr does provide conversion to unspecified-bool-type.
Yes. But this forces you do use something like:

if( ptr ) ...

Gratuous obfuscation, and forbidden by all of the coding
guidelines I've seen. (Never the less, I'd support it as well.
Because raw pointers do.)

Of course, from a QoI point of view, you wouldn't use bool, but
"Hidden const*", to avoid any chance of mis-use.

Why is it a forbidden guideline? What possible mis-use can this be?
The misuse can come from implicit conversion.
In the case of smart pointer, if the implicit conversion was bool:
shared_ptr<Foob ar;
int f=bar;

This would not be detected by the compiler because bar is implicitely
converter to bool which is promoted to int. The usual solution is to use
a pointer on member function, it is pretty safe.

Now concerning the guidelines, I always use the ==NULL notation because
the notation is more easier for me to read and it indicates me it is a
pointer that is compared. There may be other issues.

Michael
Dec 3 '07 #9
On Dec 3, 2:20 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
James Kanze a écrit :
On Dec 3, 6:36 am, Michael DOUBEZ <michael.dou... @free.frwrote:
James Kanze a écrit :
On Dec 1, 9:19 pm, David Harmon <sou...@netcom. comwrote:
On Sat, 1 Dec 2007 11:36:51 -0800 (PST) in comp.lang.c++,
Christopher <cp...@austin.r r.comwrote,
Are you referring to the boost manual?
http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
I did and all I see is a reset function which sets the internal
pointer to NULL, but it doesn't say how to check if it is NULL,
How about
if (retvalue == boost::shared_p tr<sometype>()) {
How about:
if ( retvalue == NULL ) { ... }
I've not looked at boost::shared_p tr in this regard, but all of
the intelligent pointers I've written accept it.
Boost share_ptr doesn't provide a operator==(smar t_ptr<T>& smart,T*ptr).
I'm not sure that that's what is wanted. In my own smart
pointers, I use something like:
template< typename T >
class SmartPtr
{
struct Hidden {} ;
public:
// ...
friend operator==( SmartPtr< T const&, Hidden const* ) ;
friend operator!=( Hidden const*, SmartPtr< T const& ) ;
// ...
} ;
Null pointer constants convert to Hidden const*, but the user
can't get one any other way.
Yes. Unless T is void but I guess it doesn't happen often.
Good point. That might be an issue for the Boost pointers. My
own reference counted pointers were invasive, and required the
pointed to object to derive from a specific base class. Which,
of course, void doesn't.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3152
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented after A has been defined };
6
9058
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct type:
2
7255
by: Dennis Jones | last post by:
Hello, Given something like: boost::shared_ptr<T> t( new T() ); What is the best (correct?) way to dereference the pointer? The following two methods work. Is there a difference?
2
4838
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include "B.h"
2
2137
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using namespace System; #pragma unmanaged
6
4035
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
5
2917
by: Jun | last post by:
Hello, I've code like : =========================================== class A{ public : // create print content friend std::ostream& operator<< (std::ostream& os, const A& a);
4
3962
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
10
11622
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.