Boost logo

Boost Users :

Subject: [Boost-users] [Multi-Index] Chained Ptr misunderstanding
From: Dominique Devienne (ddevienne_at_[hidden])
Date: 2009-04-13 19:16:25


Hi. I'm trying to use a shared_ptr as the value_type of a
multi_index_container, but I'm not having any success with the key
extractors (VS2005, 1.38).

>From the doc, it seems that shared_ptr would be considered a Chained
Pointer, and as such should work as-is, which by that I mean that I
think I should be able to use the built-in key extractors whether
value_type is a value or a shared_ptr. But I'm not having any luck. I
found [1] which is very similar and which Joaquín answered, but I have
other key extractors I want to use in different indexes, so I don't
think using identity<> is applicable to my case in general, although
it would be in this one. I'm trying to avoid having to write dumb
custom key extractors for Foo, which would basically replicate what
the built-in ones do in the first place.

It's most likely an error an my part, but I can't spot it. Can someone
please help? Thanks, --DD

[1] http://www.nabble.com/-multiindex--custom-key-extractor-problem-td20839161.html

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/shared_ptr.hpp>

using namespace boost;
using namespace boost::multi_index;

typedef unsigned Uid;
static Uid make_uid() {
    static Uid counter__ = 101;
    return ++counter__;
}

class Foo {
public:
    Foo() : uid_(make_uid()) {}
    const Uid& uid() const { return uid_; }

private:
    Uid uid_;
};

typedef multi_index_container<
    shared_ptr<Foo>,
    indexed_by<
        ordered_unique<
            BOOST_MULTI_INDEX_CONST_MEM_FUN(
                shared_ptr<Foo>, const Uid&, uid
            )
>
>
> FooIndex;

int main() {
    FooIndex index;
    shared_ptr<Foo> p_foo(new Foo);
    BOOST_ASSERT(index.insert(p_foo).second);
    return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net