Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 11th, 2006, 05:35 PM
yinglcs@gmail.com
Guest
 
Posts: n/a
Default A stl vector of smart pointer

In Effective STL item 8, it said 'Never create cointainers of
auto_ptrs'.

But in the Boost shared_ptr_example.cpp example, it creates a stl
vector of smart pointer.
Why it is okay in this case?

Here is part the code:
struct Foo
{
Foo( int _x ) : x(_x) {}
~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; }
int x;
/* ... */
};

typedef boost::shared_ptr<Foo> FooPtr;

struct FooPtrOps
{
bool operator()( const FooPtr & a, const FooPtr & b )
{ return a->x > b->x; }
void operator()( const FooPtr & a )
{ std::cout << a->x << "\n"; }
};

int main()
{
std::vector<FooPtr> foo_vector;
std::set<FooPtr,FooPtrOps> foo_set; // NOT multiset!

.... // omit
}

  #2  
Old January 11th, 2006, 05:45 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: A stl vector of smart pointer

<yinglcs@gmail.com> wrote in message
news:1137000350.810610.59540@z14g2000cwz.googlegro ups.com...[color=blue]
> In Effective STL item 8, it said 'Never create cointainers of
> auto_ptrs'.
>
> But in the Boost shared_ptr_example.cpp example, it creates a stl
> vector of smart pointer.[/color]

'auto_ptr' is one kind of 'smart pointer'. However, not
all 'smart pointers' are 'auto_ptr's,
[color=blue]
> Why it is okay in this case?[/color]

Find out why it's *not* OK with 'auto_ptr', and
I think you'll have your answer.

-Mike


  #3  
Old January 11th, 2006, 05:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: A stl vector of smart pointer

yinglcs@gmail.com wrote:[color=blue]
> In Effective STL item 8, it said 'Never create cointainers of
> auto_ptrs'.
>
> But in the Boost shared_ptr_example.cpp example, it creates a stl
> vector of smart pointer.
> Why it is okay in this case?
> [..][/color]

Because 'std::auto_ptr' does not satisfy the requirement imposed on
the type of the contained items.

V
  #4  
Old January 12th, 2006, 02:45 AM
Axter
Guest
 
Posts: n/a
Default Re: A stl vector of smart pointer


yinglcs@gmail.com wrote:[color=blue]
> In Effective STL item 8, it said 'Never create cointainers of
> auto_ptrs'.
>
> But in the Boost shared_ptr_example.cpp example, it creates a stl
> vector of smart pointer.
> Why it is okay in this case?
>
> Here is part the code:
> struct Foo
> {
> Foo( int _x ) : x(_x) {}
> ~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; }
> int x;
> /* ... */
> };
>
> typedef boost::shared_ptr<Foo> FooPtr;
>
> struct FooPtrOps
> {
> bool operator()( const FooPtr & a, const FooPtr & b )
> { return a->x > b->x; }
> void operator()( const FooPtr & a )
> { std::cout << a->x << "\n"; }
> };
>
> int main()
> {
> std::vector<FooPtr> foo_vector;
> std::set<FooPtr,FooPtrOps> foo_set; // NOT multiset![/color]


You could avoid having to create a FooPtrOps type class, by using a
smart pointer like copy_ptr:
http://code.axter.com/copy_ptr.h

The above smart pointer uses value semantics for the comparison
operators.
std::set<copy_ptr<Foo> > foo_set;

You can also use a cow_ptr
http://code.axter.com/cow_ptr.h

Both the copy_ptr and the cow_ptr can clone the pointee when needed.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles