Using boost::multi_index_container

Using boost::multi_index_container

本文关键字:index container multi boost Using      更新时间:2023-10-16

我一直在尝试使用boost::multi_index_container来解决我遇到的问题。但是,multi_index_container甚至无法编译声明。错误在MPL函数中很深,我不知道错误在哪里。

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)
);

在这种情况下,node_comparator 是一个 lambda,NodeType本身已经具有std::hash的特化。以下是错误的文本:

1>d:backupscodeboost_1_47_0boostmulti_indexdetailnode_type.hpp(56): error C2903: 'node_class' : symbol is neither a class template nor a function template
1>          d:backupscodeboost_1_47_0boostmplaux_preprocessedplainapply_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:backupscodeboost_1_47_0boostmplaux_preprocessedplainbind.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:backupscodeboost_1_47_0boostmplaux_preprocessedplainapply_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:backupscodeboost_1_47_0boostmplaux_preprocessedplainapply.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:backupscodeboost_1_47_0boostmplaux_preprocessedplainreverse_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:backupscodeboost_1_47_0boostmplreverse_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:backupscodeboost_1_47_0boostmulti_indexdetailnode_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:backupscodeboost_1_47_0boostmulti_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:reporenderrendersimsimcontext.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>          ]

关于原因有什么建议吗?

编辑:几乎没有上下文可言。但这里有一个SSCCE给那些离不开它的人:

#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)
    );
}

在您的SSCCE帖子之后:也许您忘记添加以下内容?

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

除此之外,我无法在这里编译您的代码,因为我的环境是 C++11 之前的环境,但我确实设法让它添加缺少的包含并用常规命名函数替换 lambda。注意我必须更改open_set的构造参数,如另一个答案中所述:

#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()
        )
    );
}

附加说明:您的 lambda 函数按值获取 NodeType,我想如果他们接受他们的参数作为 const NodeType& s,他们会更有效率。

试图在如此少的上下文中猜测问题是什么是一种有趣的消遣,但会减少您获得有用答案的机会。理想情况下,您应该提供一个完整的测试用例(即一个简短的可编译程序)来显示问题。无论如何,在黑暗中射击:你说NodeType有一个专门的std::hash专业化,但Boost.MultiIndex使用boost::hash作为其默认哈希生成器。试着看看那里。

黑暗中的另一个镜头(我认为就是这样):由于decltype(node_comparator)不是默认可构造的,因此您必须在open_set构造期间提供一个初始化值(node_comparator本身)。这似乎是您正在尝试做的事情,但不正确:正确的方法如下(模数潜在拼写错误,更多详细信息在这里):

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()
  )
);

鉴于此处使用 lambda 表达式所带来的冗长性,我认为您最好编写一个使用定义的默认可构造比较器类而不是 node_comparator .