I was trying to utilize a boost-multiindex
container for fast and positional access. Providing something like a std::vector
and an std::map
at the same time. I had no problem to construct a simple boost-multiindex
class. It worked really neat.
The problems came, as I was trying to transport my toy example to my real code. I planning to use boost-multiindex
inside a generic tree structure, where one can set the type T
of the values to store and the container in which to store the pointers to the nodes Node
at the same time.
For some reasons the commented code will not run, whatever I tried. I was unable to understand the compiler error message either. :-(
I guess it should be an easy problem, but I don't know what to do.
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/timer/timer.hpp>
struct Node {
const std::string& name() const { return mName; }
std::string mName;
};
// @brief Knoten des allgemeinen Baums, welcher Werte vom Typ T enthält.
template<typename T, template <typename Node = GenericTreeNode<T>> typename C>
struct GenericTreeNode {
typedef GenericTreeNode<T, C> self_type;
typedef T value_type;
typedef C<self_type> cont_type;
const std::string& name() const { return name_; }
std::string name_;
value_type value_;
cont_type kids_;
};
template<typename Node>
using BoostCont =
boost::multi_index::multi_index_container<
Node*,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<
boost::multi_index::const_mem_fun<Node, const std::string&, &Node::name>
>,
boost::multi_index::sequenced<>
>
>;
int main() {
// Does not not compile!
// GenericTreeNode<int, BoostCont> node;
// node.kids_.insert(new GenericTreeNode<int, BoostCont>());
// Compiles
BoostCont<Node> test;
test.insert(new Node{ "B" });
test.insert(new Node{ "A" });
test.insert(new Node{ "C" });
for (auto iter : test) {
std::cout << iter->mName << std::endl;
}
}
I'll receive the error messages:
error C2039: "name": Is not an element of "GenericTreeNode<int,BoostCont>"
error C2065: "name": undeclared identifier
error C2975: "PtrToMemberFunction": Invalid template argument for "boost::multi_index::const_mem_fun", constant compile time expression expected