From: Martin B. on 11 Nov 2009 02:50 Hi all. // With a pointer I can do: if(MyWhatever* pX = get_whatever()) { } However, for this to work with a shared pointer (shared_ptr or scoped_ptr from boost) I have to do: if(shared_ptr<MyWhatever> pX = shared_ptr<MyWhatever>(get_whatever())) { } This is ugly (and presumably could also add some runtime overhead). Is there a better way? br, Martin -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 11 Nov 2009 08:06 Martin B. wrote: > // With a pointer I can do: > if(MyWhatever* pX = get_whatever()) { > > } Correct. > However, for this to work with a shared pointer (shared_ptr or > scoped_ptr from boost) I have to do: > if(shared_ptr<MyWhatever> pX = shared_ptr<MyWhatever>(get_whatever())) { > > } Do you? What is the declaration of get_whatever()? Did you change that to return a shared_ptr, too? In that case, you could safe yourself the explicit conversion. If not, you could at least use this syntax: if(shared_ptr<T> p(get_whatever())) Which works if I remember correctly. > This is ugly (and presumably could also add some runtime overhead). > > Is there a better way? Change get_whatever() to return either a boost::shared_ptr or an std::auto_ptr. Uli -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Martin B. on 11 Nov 2009 15:49 Ulrich Eckhardt wrote: > Martin B. wrote: >> // With a pointer I can do: >> if(MyWhatever* pX = get_whatever()) { >> >> } > > Correct. > >> However, for this to work with a shared pointer (shared_ptr or >> scoped_ptr from boost) I have to do: >> if(shared_ptr<MyWhatever> pX = shared_ptr<MyWhatever>(get_whatever())) { >> >> } > > Do you? What is the declaration of get_whatever()? Did you change that to > return a shared_ptr, too? In that case, you could safe yourself the explicit > conversion. If not, you could at least use this syntax: > > if(shared_ptr<T> p(get_whatever())) > > Which works if I remember correctly. > You remember wrongly. I get a compiler error if I try this. (That was the reason for the post, if that'd work, I'd just change the syntax for the cases where I use a shared_ptr - I don't mind the missing '=' character) >> This is ugly (and presumably could also add some runtime overhead). >> >> Is there a better way? > > Change get_whatever() to return either a boost::shared_ptr or an > std::auto_ptr. > Can't change in this case. cheers, Martin -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jeff Flinn on 12 Nov 2009 01:30 Martin B. wrote: > Hi all. > > // With a pointer I can do: > if(MyWhatever* pX = get_whatever()) { > > } > > However, for this to work with a shared pointer (shared_ptr or > scoped_ptr from boost) I have to do: > if(shared_ptr<MyWhatever> pX = shared_ptr<MyWhatever>(get_whatever())) { > > } > > This is ugly (and presumably could also add some runtime overhead). > > Is there a better way? typedef shared_ptr<MyWhatever> MyWhateverPtr; // ;-) if(MyWhateverPtr pX = MyWhateverPtr(get_whatever())) { ... } or even less redundant and more safe: make get_whatever return a MyWhateverPtr. If you can't modify the get_whatever() wrap it in another function that returns a MyWhateverPtr. Jeff -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Joshua Maurice on 12 Nov 2009 09:15 On Nov 12, 10:30 am, Jeff Flinn <TriumphSprint2...(a)hotmail.com> wrote: > Martin B. wrote: > > Hi all. > > > // With a pointer I can do: > > if(MyWhatever* pX = get_whatever()) { > > > } > > > However, for this to work with a shared pointer (shared_ptr or > > scoped_ptr from boost) I have to do: > > if(shared_ptr<MyWhatever> pX = shared_ptr<MyWhatever>(get_whatever())) { > > > } > > > This is ugly (and presumably could also add some runtime overhead). > > > Is there a better way? > > typedef shared_ptr<MyWhatever> MyWhateverPtr; // ;-) > > if(MyWhateverPtr pX = MyWhateverPtr(get_whatever())) > { > ... > } > > or even less redundant and more safe: make get_whatever return a > MyWhateverPtr. If you can't modify the get_whatever() wrap it in another > function that returns a MyWhateverPtr. Adding a one line helper function to use in one case seems very much like overkill and obfuscation. What's wrong with the following? MyWhateverPtr pX(get_whatever()); if(pX.get()) { //... } If you really want to restrict the scope of pX, then add braces: { MyWhateverPtr pX(get_whatever()); if(pX.get()) { //... } } This would be much quicker and easier for me to read then look at the code and ponder what this function does, only to see it's a near trivial one liner just to play to your particular liking for putting declarations in if statements. just my humble opinion. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 Prev: Arguement against NULLing a pointer after delete? Next: Virtual inheritance and typedef |