Connecting Tech Pros Worldwide Help | Site Map

How to detect NULL for boost:shared_ptr

tradevol@yahoo.com
Guest
 
Posts: n/a
#1: Aug 4 '08
Hi,

I am playing with boost pointer and try to wrap the following codes

A* func(){
...
if(condition 1 ){
return a;
}
else
return NULL;

}

Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.

How should I do it in this case?

Thanks

Chris

Victor Bazarov
Guest
 
Posts: n/a
#2: Aug 4 '08

re: How to detect NULL for boost:shared_ptr


tradevol@yahoo.com wrote:
Quote:
I am playing with boost pointer and try to wrap the following codes
>
A* func(){
...
if(condition 1 ){
return a;
}
else
return NULL;
>
}
>
Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.
What does it mean that it "will not accept" it? Do you get a compiler
error? Do you get a run-time exception? If the latter, catch it. If
the former, deal with it (see FAQ 5.8).
Quote:
>
How should I do it in this case?
Read the FAQ.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thomas J. Gritzan
Guest
 
Posts: n/a
#3: Aug 4 '08

re: How to detect NULL for boost:shared_ptr


tradevol@yahoo.com schrieb:
Quote:
Hi,
>
I am playing with boost pointer and try to wrap the following codes
>
A* func(){
...
if(condition 1 ){
return a;
}
else
return NULL;
>
}
>
Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.
>
How should I do it in this case?
Did you try to return a default-constructed pointer like this?

return shared_ptr<A>();

--
Thomas
sylvester.zaluga@googlemail.com
Guest
 
Posts: n/a
#4: Aug 4 '08

re: How to detect NULL for boost:shared_ptr


On 4 Sie, 21:37, "Thomas J. Gritzan" <phygon_antis...@gmx.dewrote:
Quote:
trade...@yahoo.com schrieb:
>
>
>
Quote:
Hi,
>
Quote:
I am playing with boost pointer and try to wrap the following codes
>
Quote:
A* func(){
* *...
* *if(condition 1 ){
* * * return a;
* }
* else
* *return NULL;
>
Quote:
}
>
Quote:
Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.
>
Quote:
How should I do it in this case?
>
Did you try to return a default-constructed pointer like this?
>
* *return shared_ptr<A>();
>
--
Thomas
Well, I don't know exactly what you mean, since shared_ptr is there to
avoid the ordinary pointers. However, if I understand correctly what
this is all about you can do it this way:

shared_ptr<Afunc()
{
shared_ptr<Ar; //automatically set to point to NULL
if(condition 1 )
{
shared_ptr<Ar(new A);
return r; //I read somewhere to avoid creating new shared_ptrs
"on the fly" like "return shared_ptr<A>(new A);"
}
else
{
shared_ptr<Ar; //set to point to NULL automatically
return r;
}

}

// ... and then, somewhere in code:
shared_ptr<Atest = func();
if(test.get() == NULL)
do sth



I hope it helps...
tradevol@yahoo.com
Guest
 
Posts: n/a
#5: Aug 4 '08

re: How to detect NULL for boost:shared_ptr


On Aug 4, 5:01*pm, sylvester.zal...@googlemail.com wrote:
Quote:
On 4 Sie, 21:37, "Thomas J. Gritzan" <phygon_antis...@gmx.dewrote:
>
>
>
Quote:
trade...@yahoo.com schrieb:
>
Quote:
Quote:
Hi,
>
Quote:
Quote:
I am playing with boost pointer and try to wrap the following codes
>
Quote:
Quote:
A* func(){
* *...
* *if(condition 1 ){
* * * return a;
* }
* else
* *return NULL;
>
Quote:
Quote:
}
>
Quote:
Quote:
Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.
>
Quote:
Quote:
How should I do it in this case?
>
Quote:
Did you try to return a default-constructed pointer like this?
>
Quote:
* *return shared_ptr<A>();
>
Quote:
--
Thomas
>
Well, I don't know exactly what you mean, since shared_ptr is there to
avoid the ordinary pointers. However, if I understand correctly what
this is all about you can do it this way:
>
shared_ptr<Afunc()
{
* *shared_ptr<Ar; //automatically set to point to NULL
* *if(condition 1 )
* *{
* * * shared_ptr<Ar(new A);
* * * return r; //I read somewhere to avoid creating new shared_ptrs
"on the fly" like "return shared_ptr<A>(new A);"
* }
* else
*{
* * *shared_ptr<Ar; //set to point to NULL automatically
* * *return r;
*}
>
}
>
// ... and then, somewhere in code:
shared_ptr<Atest = func();
if(test.get() == NULL)
* * do sth
>
I hope it helps...
yes. This what I asked for. sorry for the confusion.
Thank you very much
Noah Roberts
Guest
 
Posts: n/a
#6: Aug 4 '08

re: How to detect NULL for boost:shared_ptr


sylvester.zaluga@googlemail.com wrote:
Quote:
shared_ptr<Afunc()
{
shared_ptr<Ar; //automatically set to point to NULL
if(condition 1 )
{
shared_ptr<Ar(new A);
This is an unnecessary stack variable. Same with below. You can call
reset(T * p) to assign to the pointer declared in function scope.
Quote:
return r; //I read somewhere to avoid creating new shared_ptrs
"on the fly" like "return shared_ptr<A>(new A);"
}
else
{
shared_ptr<Ar; //set to point to NULL automatically
return r;
}
>
}
James Kanze
Guest
 
Posts: n/a
#7: Aug 5 '08

re: How to detect NULL for boost:shared_ptr


On Aug 4, 10:03 pm, trade...@yahoo.com wrote:
Quote:
I am playing with boost pointer and try to wrap the following codes
>
A* func(){
...
if(condition 1 ){
return a;
}
else
return NULL;
}
Quote:
Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.
Are you sure? I've not much experience with boost::shared_ptr,
but all of the smart pointers I've use do accept constructing
them with NULL.
Quote:
How should I do it in this case?
In this particular case, I'd probably write something like:

shared_ptr< A >
func()
{
return shared_ptr< A >(
someCondition
? new A
: NULL ) ;
}

In more complicated cases, however:

shared_ptr< A >
func()
{
shared_ptr< A result ;
if ( someCondition ) {
result = new A ;
}
return result ;
}

I'd be very surprised if either of them failed to work with
boost::shared_ptr. Just as I'd be very surprised if, given the
definition of result, above "result = NULL ;" wouldn't compile;
for that matter, there's no reason why "if ( result == NULL )"
can't be made to work as expected, either.

--
James Kanze (GABI Software) email:james.kanze@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
Pete Becker
Guest
 
Posts: n/a
#8: Aug 5 '08

re: How to detect NULL for boost:shared_ptr


On 2008-08-05 04:17:07 -0400, James Kanze <james.kanze@gmail.comsaid:
Quote:
On Aug 4, 10:03 pm, trade...@yahoo.com wrote:
>
Quote:
>The problem is that shared_ptr will not accept NULL.
>
Are you sure? I've not much experience with boost::shared_ptr,
but all of the smart pointers I've use do accept constructing
them with NULL.
Yes, it's a problem. The constructor that takes a raw pointer is a template:

template <class Tclass shared_ptr {
template <class Yexplicit shared_ptr(Y *p);
...
};

with a requirement that Y* must be convertible to T*. So a normal null
pointer doesn't work. For a shared_ptr<intyou have to use something
like (int*)0, or use the default constructor.

In C++0x there's a new constructor that takes an argument of type nullptr_t.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

James Kanze
Guest
 
Posts: n/a
#9: Aug 5 '08

re: How to detect NULL for boost:shared_ptr


On Aug 5, 12:51 pm, Pete Becker <p...@versatilecoding.comwrote:
Quote:
On 2008-08-05 04:17:07 -0400, James Kanze <james.ka...@gmail.comsaid:
Quote:
Quote:
On Aug 4, 10:03 pm, trade...@yahoo.com wrote:
Quote:
Quote:
Quote:
The problem is that shared_ptr will not accept NULL.
Quote:
Quote:
Are you sure? I've not much experience with boost::shared_ptr,
but all of the smart pointers I've use do accept constructing
them with NULL.
Quote:
Yes, it's a problem. The constructor that takes a raw pointer
is a template:
Quote:
template <class Tclass shared_ptr {
template <class Yexplicit shared_ptr(Y *p);
...
};
And there's not a non-templated version alongside of it?
Sounds like something got forgotten along the way.
Quote:
with a requirement that Y* must be convertible to T*. So a
normal null pointer doesn't work. For a shared_ptr<intyou
have to use something like (int*)0, or use the default
constructor.
But it would be easy to add a constructor which would work with
NULL. Which is what most users would expect.

Hmmm. I don't see in Boost documentation where they support
comparison with NULL, either. Which would seem a rather
frequent operation, and certainly necessary if you're doing like
the original poster, and converting from raw pointers.

--
James Kanze (GABI Software) email:james.kanze@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
Kevin Frey
Guest
 
Posts: n/a
#10: Aug 6 '08

re: How to detect NULL for boost:shared_ptr


Hmmm. I don't see in Boost documentation where they support
Quote:
comparison with NULL, either. Which would seem a rather
frequent operation, and certainly necessary if you're doing like
the original poster, and converting from raw pointers.
You may not be able to compare to NULL explicitly (this is unconfirmed by
me), but shared_ptr has the operator unspecified_bool_type( ) so you can
test the pointer directly, so it's really not that big a deal (OTHER than
the fact that a shared_ptr is not syntactically a "drop-in" replacement for
a conventional pointer).

shared_ptr< MyClass myVar = func( );

if( myVar ) // is the pointer not null?
DoSomething( );



James Kanze
Guest
 
Posts: n/a
#11: Aug 6 '08

re: How to detect NULL for boost:shared_ptr


On Aug 6, 2:32 am, "Kevin Frey" <kevin_g_f...@hotmail.comwrote:
Quote:
Quote:
Hmmm. I don't see in Boost documentation where they support
comparison with NULL, either. Which would seem a rather
frequent operation, and certainly necessary if you're doing like
the original poster, and converting from raw pointers.
Quote:
You may not be able to compare to NULL explicitly (this is unconfirmed by
me), but shared_ptr has the operator unspecified_bool_type( ) so you can
test the pointer directly, so it's really not that big a deal (OTHER than
the fact that a shared_ptr is not syntactically a "drop-in" replacement for
a conventional pointer).
Quote:
shared_ptr< MyClass myVar = func( );
Quote:
if( myVar ) // is the pointer not null?
DoSomething( );
In other words, it supports the misfeatures of standard
pointers, but not the correct way of using them. (Obviously,
if you're going to call it a smart pointer, you have to support
the misfeatures as well. But not supporting the standard idiom
is simply not acceptable.)

--
James Kanze (GABI Software) email:james.kanze@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
Closed Thread