assign shared_ptr to 0 in initialization list

Hello, I am in the process of converting an application that uses raw pointers to boost::shared_ptr. For example: ErrorWriter *writer_; now becomes: typedef boost::shared_ptr<ErrorWriter> ErrorWriterPtr; ErrorWriterPtr writer_; The problem is that, with raw pointers I was able to use 0-value pointers in the constructor, like so: Parser(ErrorWriter *writer) : writer_(writer) { } Parser testParser(0); So it was possible to initialize the class with an empty writer pointer, and then later in some other Parser code do this: if (writer_ == 0) { /* create default error writer */ } But now, with shared_ptr, I cannot assign or evaluate to 0. I understand that the default ErrorWriterPtr constructor and ErrorWriterPtr.reset() will provide me with an empty pointer that I can check like so: if (writer_.get() == 0) { /* create default error writer */ } But how do I rewrite the Parser constructor to fit this new idiom? If I have: Parser(ErrorWriterPtr writer) : writer_(writer) { } What is the equivalent to: Parser testParser(0); Thank you for any assistance! Best, Scott

Parser(ErrorWriterPtr writer) : writer_(writer) { }
What is the equivalent to:
Parser testParser(0); I believe that this constructor would work for you.
Parser(ErrorWriter* writer) : writer_(writer) { } I hope that is helpful. If not I'm sure someone else will have some suggestions. Kind regards, Eoin.

On 8/13/06, Scott Amort <jsamort@gmail.com> wrote:
But now, with shared_ptr, I cannot assign or evaluate to 0. I understand that the default ErrorWriterPtr constructor and ErrorWriterPtr.reset() will provide me with an empty pointer that I can check.
Instead of
if (writer_.get() == 0) { /* create default error writer */ } Why not just use if ( !writer_ ) { /* create default error writer */ } instead? This would work with ordinary pointers too.
What is the equivalent to:
Parser testParser(0);
Parser testParser( ErrorWriterPtr() ); // would work I'd be tempted instead to replace the constructor definition with Parser(ErrorWriterPtr writer = ErrorWritePtr()) : writer_(writer) { } however, to be able to just say Parser testParser; if you don't want to specify the writer. ~ Scott McMurray

Hi Scott, Thanks for the quick reply! On Sun, 2006-08-13 at 18:11 -0400, me22 wrote:
Instead of
if (writer_.get() == 0) { /* create default error writer */ } Why not just use if ( !writer_ ) { /* create default error writer */ } instead? This would work with ordinary pointers too.
Thanks, that is clearer!
What is the equivalent to:
Parser testParser(0);
Parser testParser( ErrorWriterPtr() ); // would work
I'd be tempted instead to replace the constructor definition with Parser(ErrorWriterPtr writer = ErrorWritePtr()) : writer_(writer) { } however, to be able to just say Parser testParser; if you don't want to specify the writer.
Perfect! Thanks again. Best, Scott

Hi Ion, I think that the shmem docs have a bug. Section 'A shared memory pointer: offset_ptr' mentions '(reinterpret_cast<char>(&offset_ptr))+1' which I would change for '(reinterpret_cast<char*>(&offset_ptr))+1' (notice the char*) Otherwise if it's not a bug how do you assign or test against NULL for a offset_1_null_ptr?? Thanks in advance.

Hi Berenguer,
I think that the shmem docs have a bug.
Section 'A shared memory pointer: offset_ptr' mentions '(reinterpret_cast<char>(&offset_ptr))+1' which I would change for '(reinterpret_cast<char*>(&offset_ptr))+1' (notice the char*)
You are right, it should be "char*". It's the address of the offset_ptr object plus 1. Thanks, and regards, Ion

Hi, I've been following this thread and been unable to do the same with: boost::shared_ptr< map<string,string> > () So I can't do: constructor ( boost::shared_ptr< map<string,string> > varName = boost::shared_ptr< map<string,string> > () ) The error I get is (why/how do I specify the missing params for map?): [cc] /home/bereng/blaseek/src/lib/cppToolkit/DBHelper.h:61: error: expected a,a or a...a before a>a token [cc] /home/bereng/blaseek/src/lib/cppToolkit/DBHelper.h:61: error: wrong number of template arguments (1, should be 4) [cc] /usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/stl_ map.h:92: error: provided for atemplate<class _Key, class _Tp, class _Compare, class _Alloc> class std::mapa [cc] /home/bereng/blaseek/src/lib/cppToolkit/DBHelper.h:61: error: template argument 1 is invalid [cc] /home/bereng/blaseek/src/lib/cppToolkit/DBHelper.h:61: error: default argument missing for parameter 2 of aboost::shared_ptr<cppToolkit::ResultSet> cppToolkit::DBHelper::findObject(boost::shared_ptr<std::map<std::basic_strin g<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >
, std::string)a
Thanks a lot in advance. -----Mensaje original----- De: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org]En nombre de Scott Amort Enviado el: lunes, 14 de agosto de 2006 1:09 Para: boost-users@lists.boost.org Asunto: Re: [Boost-users] assign shared_ptr to 0 in initialization list Hi Scott, Thanks for the quick reply! On Sun, 2006-08-13 at 18:11 -0400, me22 wrote:
Instead of
if (writer_.get() == 0) { /* create default error writer */ } Why not just use if ( !writer_ ) { /* create default error writer */ } instead? This would work with ordinary pointers too.
Thanks, that is clearer!
What is the equivalent to:
Parser testParser(0);
Parser testParser( ErrorWriterPtr() ); // would work
I'd be tempted instead to replace the constructor definition with Parser(ErrorWriterPtr writer = ErrorWritePtr()) : writer_(writer) { } however, to be able to just say Parser testParser; if you don't want to specify the writer.
Perfect! Thanks again. Best, Scott _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (5)
-
Berenguer Blasi
-
Eoin
-
Ion Gaztañaga
-
me22
-
Scott Amort