boost::shared_ptr and NULL

In this function declaration, is there a simpler way to express a null shared pointer as the default argument value? void foo( const boost::shared_ptr<T> & p = boost::shared_ptr<T>( ) ); In this function call, is there a simpler way to express a null shared pointer as the actual argument? foo( boost::shared_ptr<T>( ) ); ----------------------------------------------------------------------- Andrew R. Thomas-Cramer Work: artc@prism-cs.com http://www.prism-cs.com/ Home: artc@execpc.com http://www.execpc.com/~artc/ ----------------------------------------------------------------------- [Non-text portions of this message have been removed]

From: "Andrew R. Thomas-Cramer" <artc@prism-cs.com>
In this function declaration, is there a simpler way to express a null
shared pointer as the default argument value?
void foo( const boost::shared_ptr<T> & p = boost::shared_ptr<T>( ) );
In this function call, is there a simpler way to express a null shared pointer as the actual argument? foo( boost::shared_ptr<T>( ) );
No to both. In 1.27 boost::shared_ptr can be constructed from the literal 0, but the constructor is explicit. In 1.28, the literal 0 is no longer a valid argument to the constructor.

----- Original Message ----- From: "Peter Dimov" <pdimov@mmltd.net> To: <Boost-Users@yahoogroups.com> Sent: Monday, May 13, 2002 1:44 PM Subject: Re: [Boost-Users] boost::shared_ptr and NULL
From: "Andrew R. Thomas-Cramer" <artc@prism-cs.com>
In this function declaration, is there a simpler way to express a null
shared pointer as the default argument value?
void foo( const boost::shared_ptr<T> & p = boost::shared_ptr<T>( ) );
In this function call, is there a simpler way to express a null shared pointer as the actual argument? foo( boost::shared_ptr<T>( ) );
No to both. In 1.27 boost::shared_ptr can be constructed from the literal 0, but the constructor is explicit. In 1.28, the literal 0 is no longer a valid argument to the constructor.
Has there been any discussion in the past about supporting a more concise/readable value? E.g., the second call below appears more readable to me. (This particular example relies on "boost::null" being a class with a default constructor, and boost::shared_ptr providing a non-explicit constructor accepting a boost::null instance as an argument; this may be flawed.) foo( boost::shared_ptr<MyVeryLongClassName>() ); foo( boost::null() );

From: "Andrew R. Thomas-Cramer" <artc@prism-cs.com>
Has there been any discussion in the past about supporting a more concise/readable value? E.g., the second call below appears more readable
to me.
(This particular example relies on "boost::null" being a class with a default constructor, and boost::shared_ptr providing a non-explicit constructor accepting a boost::null instance as an argument; this may be flawed.)
foo( boost::shared_ptr<MyVeryLongClassName>() ); foo( boost::null() );
No, I don't recall any discussion about it. I haven't encountered this problem myself; I typically overload foo so that the call would be simply foo(); This doesn't extend well to multiple optional shared_ptr arguments, of course.

As an alternative, I usually find that the "Introduce Null Object" refactoring results in simpler and more readable code overall. Perhaps not as concise though. ----- Original Message ----- From: Andrew R. Thomas-Cramer To: Boost-Users@yahoogroups.com Sent: Monday, May 13, 2002 12:50 PM Subject: Re: [Boost-Users] boost::shared_ptr and NULL ----- Original Message ----- From: "Peter Dimov" <pdimov@mmltd.net> To: <Boost-Users@yahoogroups.com> Sent: Monday, May 13, 2002 1:44 PM Subject: Re: [Boost-Users] boost::shared_ptr and NULL
From: "Andrew R. Thomas-Cramer" <artc@prism-cs.com>
In this function declaration, is there a simpler way to express a null
shared pointer as the default argument value?
void foo( const boost::shared_ptr<T> & p = boost::shared_ptr<T>( ) );
In this function call, is there a simpler way to express a null shared pointer as the actual argument? foo( boost::shared_ptr<T>( ) );
No to both. In 1.27 boost::shared_ptr can be constructed from the literal 0, but the constructor is explicit. In 1.28, the literal 0 is no longer a valid argument to the constructor.
Has there been any discussion in the past about supporting a more concise/readable value? E.g., the second call below appears more readable to me. (This particular example relies on "boost::null" being a class with a default constructor, and boost::shared_ptr providing a non-explicit constructor accepting a boost::null instance as an argument; this may be flawed.) foo( boost::shared_ptr<MyVeryLongClassName>() ); foo( boost::null() ); Yahoo! Groups Sponsor ADVERTISEMENT Info: <http://www.boost.org> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> Unsubscribe: <mailto:boost-users-unsubscribe@yahoogroups.com> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. [Non-text portions of this message have been removed]

----- Original Message ----- From: "Sean Shubin" <sshubin@socal.rr.com> To: <Boost-Users@yahoogroups.com> Sent: Tuesday, May 14, 2002 3:25 AM Subject: Re: [Boost-Users] boost::shared_ptr and NULL
As an alternative, I usually find that the "Introduce Null Object" refactoring results in simpler and more readable code overall. Perhaps not as concise though.
I'm not familiar with that term. Could you define it and provide an example?

From the book "Refactoring"
Sometimes you can simplify a conditional expression such as this: class Foo { void foo() { ... } }; ... x = 0; ... if(x == 0) handleNullCondition(); //conditional expression else x->foo(); with something like this: class Foo { void foo() { ... } }; class NullFoo { void foo() { handleNullCondition(); } }; ... x = new NullFoo(); //instead of x = 0 ... x->foo(); //conditional expression goes away //polymorphism handles it instead So if I only have one check for class Foo being null, the code becomes bigger (I create a new class). But if I had many checks all over the place, it actually simplifies the code (a lot of if statements go away). ----- Original Message ----- From: Andrew R. Thomas-Cramer To: Boost-Users@yahoogroups.com Sent: Tuesday, May 14, 2002 7:11 AM Subject: Re: [Boost-Users] boost::shared_ptr and NULL ----- Original Message ----- From: "Sean Shubin" <sshubin@socal.rr.com> To: <Boost-Users@yahoogroups.com> Sent: Tuesday, May 14, 2002 3:25 AM Subject: Re: [Boost-Users] boost::shared_ptr and NULL
As an alternative, I usually find that the "Introduce Null Object" refactoring results in simpler and more readable code overall. Perhaps not as concise though.
I'm not familiar with that term. Could you define it and provide an example? Yahoo! Groups Sponsor ADVERTISEMENT Info: <http://www.boost.org> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> Unsubscribe: <mailto:boost-users-unsubscribe@yahoogroups.com> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. [Non-text portions of this message have been removed]
participants (3)
-
Andrew R. Thomas-Cramer
-
Peter Dimov
-
Sean Shubin