Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-05-25 06:50:50


From: "Gary Powell" <Gary.Powell_at_[hidden]>

> I have a table of shared_ptrs<string>, and I used these as keys in a map.
> Not too unreasonable. However not all programmers on my team will use this
> table of strings, so I have to construct a shared_ptr<string> from other
> char*. (The alternative is to find the char * in the table and then use
that
> shared_ptr for the lookup.) They don't match of course and I'm dead in the
> water.

Because you were comparing shared_ptr identity, not the contents of the
string. This is similar to what happens when you use const char* as a map
key.

> However it compiles and runs, it just doesn't do what I expect. (Of
> course I am guilty of not fully reading the documentation, and the
source.)

Yes.

> So now I'm left with overloading less<shared_ptr<string> >, or creating a
> proxy that has an operator<().

What's wrong with just using your own comparison object:
map<boost::shared_ptr<std::string>, compare_string_shared_ptr>

> I chose the proxy to avoid other problems and
> it got progressively messier. So in the end, I tossed using shared_ptrs as
> keys.
>
> And while I don't consider myself a newbie, I still made this mistake.
What
> hope does a new user of shared_ptrs have?

It is apparent to me that you were unable to take advantage of this library
facility to solve your problem. Does that mean that the library facility
solves no problems and should be removed? I don't think so.

-Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk