Puppy
Puppy

Reputation: 146910

Using boost::multi_index_container

I've been attempting to use boost::multi_index_container to solve a problem I'm having. However, the multi_index_container fails to compile even the declaration. The error is deep in an MPL function and I have little idea where the fault lies.

boost::multi_index_container<
    NodeType,
    boost::multi_index::indexed_by<
        boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>, decltype(node_comparator)>,
        boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>
    >
> open_set(
    boost::make_tuple(node_comparator)
);

In this case, node_comparator is a lambda, and NodeType itself already has a std::hash specialization. Here's the text of the error:

1>d:\backups\code\boost_1_47_0\boost\multi_index\detail\node_type.hpp(56): error C2903: 'node_class' : symbol is neither a class template nor a function template
1>          d:\backups\code\boost_1_47_0\boost\mpl\aux_\preprocessed\plain\apply_wrap.hpp(49) : see reference to class template instantiation 'boost::multi_index::detail::index_node_applier::apply<IndexSpecifierIterator,Super>' being compiled
1>          with
1>          [
1>              IndexSpecifierIterator=boost::mpl::v_iter<boost::mpl::vector2<boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>,Wide::Sim::`anonymous-namespace'::<lambda8>>,boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>>,0>,
1>              Super=boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<NodeType,std::allocator<NodeType>>>
1>          ]
1>          d:\backups\code\boost_1_47_0\boost\mpl\aux_\preprocessed\plain\bind.hpp(207) : see reference to class template instantiation 'boost::mpl::apply_wrap2<F,T1,T2>' being compiled
1>          with
1>          [
1>              F=boost::multi_index::detail::index_node_applier,
1>              T1=boost::mpl::v_iter<boost::mpl::vector2<boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>,Wide::Sim::`anonymous-namespace'::<lambda8>>,boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>>,0>,
1>              T2=boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<NodeType,std::allocator<NodeType>>>
1>          ]
1>          d:\backups\code\boost_1_47_0\boost\mpl\aux_\preprocessed\plain\apply_wrap.hpp(49) : see reference to class template instantiation 'boost::mpl::bind2<F,T1,T2>::apply<U1,U2>' being compiled
1>          with
1>          [
1>              F=boost::multi_index::detail::index_node_applier,
1>              T1=boost::mpl::_2,
1>              T2=boost::mpl::_1,
1>              U1=boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<NodeType,std::allocator<NodeType>>>,
1>              U2=boost::mpl::v_iter<boost::mpl::vector2<boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>,Wide::Sim::`anonymous-namespace'::<lambda8>>,boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>>,0>
1>          ]
1>          d:\backups\code\boost_1_47_0\boost\mpl\aux_\preprocessed\plain\apply.hpp(63) : see reference to class template instantiation 'boost::mpl::apply_wrap2<F,T1,T2>' being compiled
1>          with
1>          [
1>              F=boost::mpl::bind2<boost::multi_index::detail::index_node_applier,boost::mpl::_2,boost::mpl::_1>,
1>              T1=boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<NodeType,std::allocator<NodeType>>>,
1>              T2=boost::mpl::v_iter<boost::mpl::vector2<boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>,Wide::Sim::`anonymous-namespace'::<lambda8>>,boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>>,0>
1>          ]
1>          d:\backups\code\boost_1_47_0\boost\mpl\aux_\preprocessed\plain\reverse_iter_fold_impl.hpp(82) : see reference to class template instantiation 'boost::mpl::apply2<F,T1,T2>' being compiled
1>          with
1>          [
1>              F=boost::mpl::bind2<boost::multi_index::detail::index_node_applier,boost::mpl::_2,boost::mpl::_1>,
1>              T1=boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<NodeType,std::allocator<NodeType>>>,
1>              T2=boost::mpl::v_iter<boost::mpl::vector2<boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>,Wide::Sim::`anonymous-namespace'::<lambda8>>,boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>>,0>
1>          ]
1>          d:\backups\code\boost_1_47_0\boost\mpl\reverse_iter_fold.hpp(43) : see reference to class template instantiation 'boost::mpl::aux::reverse_iter_fold_impl<N,First,Last,State,BackwardOp,ForwardOp>' being compiled
1>          with
1>          [
1>              N=2,
1>              First=boost::mpl::v_iter<boost::mpl::vector2<boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>,Wide::Sim::`anonymous-namespace'::<lambda8>>,boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>>,0>,
1>              Last=boost::mpl::v_iter<boost::mpl::vector2<boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>,Wide::Sim::`anonymous-namespace'::<lambda8>>,boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>>,2>,
1>              State=boost::multi_index::detail::index_node_base<NodeType,std::allocator<NodeType>>,
1>              BackwardOp=boost::mpl::bind2<boost::multi_index::detail::index_node_applier,boost::mpl::_2,boost::mpl::_1>,
1>              ForwardOp=boost::mpl::protect<boost::mpl::arg<1>>
1>          ]
1>          d:\backups\code\boost_1_47_0\boost\multi_index\detail\node_type.hpp(70) : see reference to class template instantiation 'boost::mpl::reverse_iter_fold<Sequence,State,BackwardOp>' being compiled
1>          with
1>          [
1>              Sequence=boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>,Wide::Sim::`anonymous-namespace'::<lambda8>>,boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>>,
1>              State=boost::multi_index::detail::index_node_base<NodeType,std::allocator<NodeType>>,
1>              BackwardOp=boost::mpl::bind2<boost::multi_index::detail::index_node_applier,boost::mpl::_2,boost::mpl::_1>
1>          ]
1>          d:\backups\code\boost_1_47_0\boost\multi_index_container.hpp(75) : see reference to class template instantiation 'boost::multi_index::detail::multi_index_node_type<Value,IndexSpecifierList,Allocator>' being compiled
1>          with
1>          [
1>              Value=NodeType,
1>              IndexSpecifierList=boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>,Wide::Sim::`anonymous-namespace'::<lambda8>>,boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>>,
1>              Allocator=std::allocator<NodeType>
1>          ]
1>          c:\repo\render\render\sim\simcontext.cpp(264) : see reference to class template instantiation 'boost::multi_index::multi_index_container<Value,IndexSpecifierList>' being compiled
1>          with
1>          [
1>              Value=NodeType,
1>              IndexSpecifierList=boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>,Wide::Sim::`anonymous-namespace'::<lambda8>>,boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>>
1>          ]

Any suggestions as to the cause?

Edit: There's pretty much no context to be had. But here's an SSCCE for those of you who can't live without one:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>

struct NodeType {
    int x, y, z;
    float g_score;
    NodeType(int ax, int ay, int az) {
        x = ax;
        y = ay;
        z = az;        
    }
    NodeType() {}
    bool operator==(const NodeType& other) const {
        return x == other.x && y == other.y && z == other.z;
    }
};

template<> struct std::hash<NodeType> : public std::unary_function<const NodeType&, std::size_t> {
    std::size_t operator()(const NodeType& node) const {
        return std::hash<int>()(node.x * 100000 + node.y * 1000 + node.z);
    }
};
template<> struct boost::hash<NodeType> : public std::unary_function<const NodeType&, std::size_t> {
    std::size_t operator()(const NodeType& node) const {
        return std::hash<int>()(node.x * 100000 + node.y * 1000 + node.z);
    }
};

int main() {
    auto h = [&](NodeType x) {
        return 5.0f; // details irrelevant
    };    
    auto node_comparator = [&](NodeType lhs, NodeType rhs) {
        return lhs.g_score + h(lhs) < rhs.g_score + h(rhs);
    };
    boost::multi_index_container<
        NodeType,
        boost::multi_index::indexed_by<
            boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>, decltype(node_comparator)>,
            boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>
        >
    > open_set(
        boost::make_tuple(node_comparator)
    );
}

Upvotes: 2

Views: 1813

Answers (3)

After your SSCCE post: maybe you forgot to add the following?

#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>

Other than that, I can't compile your code here because my environment is pre-C++11, but I did manage to make it work adding the missing includes and replacing lambdas with regular named functions. Note I had to change the construction args of open_setas explained in another answer:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>

struct NodeType {
    int x, y, z;
    float g_score;
    NodeType(int ax, int ay, int az) {
        x = ax;
        y = ay;
        z = az;        
    }
    NodeType() {}
    bool operator==(const NodeType& other) const {
        return x == other.x && y == other.y && z == other.z;
    }
};

template<> struct boost::hash<NodeType> : public std::unary_function<const NodeType&,     std::size_t> {
    std::size_t operator()(const NodeType& node) const {
        return boost::hash<int>()(node.x * 100000 + node.y * 1000 + node.z);
    }
};

double h(NodeType x) {
    return 5.0f;
}

bool node_comparator(NodeType lhs, NodeType rhs) {
    return lhs.g_score + h(lhs) < rhs.g_score + h(rhs);
}

int main() {
    typedef boost::multi_index_container<
        NodeType,
        boost::multi_index::indexed_by<
            boost::multi_index::ordered_non_unique<boost::multi_index::identity<NodeType>, bool (*)(NodeType,NodeType)>,
            boost::multi_index::hashed_unique<boost::multi_index::identity<NodeType>>
        >
    > open_set_t;

    open_set_t open_set(
        boost::make_tuple(
            boost::make_tuple(
                boost::multi_index::identity<NodeType>(),
                &node_comparator
            ),
            open_set_t::nth_index<1>::type::ctor_args()
        )
    );
}

Additional note: your lambda functions are taking NodeTypes by value, I guess they'd be more efficient if they accepted their arguments as const NodeType&s.

Upvotes: 2

Another shot in the dark (i think this is it): as decltype(node_comparator) is not default constructible you have to provide an initialization value (node_comparator itself) during open_setconstruction. This is what seemingly you're trying to do, but incorrectly: the right way is as follows (modulo potential typos, more details here):

typedef boost::multi_index_container<
  NodeType,
  ...
> open_set_t;

open_set_t open_set(
  boost::make_tuple(
    boost::make_tuple(
      boost::multi_index::identity<NodeType>(),
      node_comparator
    ),
    open_set_t::nth_index<1>::type::ctor_args()
  )
);

Given the verbosity imposed by the use of a lambda expression here, I think you're better off by writing a used-defined, default constructible comparator class instead of node_comparator.

Upvotes: 0

Trying to guess what the problem is with so little context makes for a fun pastime but reduces your chances that you get a useful answer. Ideally you should be providing a complete testcase (i.e. a short compilable program) showing the issue. Anyway, a shot in the dark: you say that NodeTypehas a dedicated std::hash specialization, but Boost.MultiIndex uses boost::hash as its default hash generator. Try looking there.

Upvotes: 0

Related Questions