Boost logo

Boost Users :

Subject: [Boost-users] Pointer to objects in Boost.MultiIndex
From: Amit Prakash Ambasta (amit.prakash.ambasta_at_[hidden])
Date: 2015-12-30 03:34:00


Consider the following code snippet:

#include <iostream>

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>

using namespace std;
using namespace boost::multi_index;

struct LinkList;

struct LinkList {
    size_t index;
    LinkList* parent;
    LinkList(size_t index, LinkList* parent) : index(index), parent(parent)
{}
};

struct llindex {};

typedef ordered_unique<tag<llindex>, member<LinkList, size_t,
&LinkList::index> > LinkListIndex;
typedef boost::multi_index_container<LinkList, indexed_by<LinkListIndex> >
IndexedLinkList;

template <typename T> void show(T& iterable) {
    for(auto const& element: iterable) {
        if(element.parent != nullptr) {
            std::cout << "Index<" << element.index << ">: " <<
element.parent->index << std::endl;
        }
        else {
            std::cout << "Index<" << element.index << ">: " << "NULL" <<
std::endl;
        }
    }
}

int main() {
    IndexedLinkList ill;
    auto &index_ll = ill.get<0>();

    ill.insert(LinkList{0, nullptr});
    ill.insert(LinkList{1, (LinkList*)(&(*index_ll.find(0)))});
    ill.insert(LinkList{2, (LinkList*)(&(*index_ll.find(0)))});
    ill.insert(LinkList{3, (LinkList*)(&(*index_ll.find(2)))});

    show(ill);
    return 0;
}

Would (LinkList*)(&(*index__ll.find(value))) be the recommended way to
fetch the pointer to parent?



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