【发布时间】:2011-04-25 12:25:40
【问题描述】:
我有一个boost::bimap<int, boost::shared_ptr<A>> 容器并希望将一个迭代器返回到左视图以确保内容的一致性。返回container.left.begin() 会返回一个取消引用std::pair<int const, boost::shared_ptr<A> const> 的迭代器。
显然,这不是我想要的,因为可以通过取消引用 shared_ptr 来更改 A。我想要一个取消引用 std::pair<int const, boost::shared_ptr<A const>> 的迭代器(我并不关心 shared_ptr 是否为 const)。我知道我可能应该使用boost::transform_iterator 来执行此操作,但我无法弄清楚“强制转换”功能应该是什么样子。
有人可以帮我解决这个问题吗?还是有其他更简单的方法来获得我想要的东西?
编辑:这是我目前所拥有的,我只想说,它给了我 2 个屏幕的错误。
typedef boost::bimap<unsigned int, boost::shared_ptr<A> > container_type;
typedef container_type::left_const_iterator base_const_iterator;
typedef boost::transform_iterator<makeIterConst<A>, base_const_iterator> const_iterator;
template <typename T>
struct makeIterConst : std::unary_function<std::pair<unsigned int const, boost::shared_ptr<T> const>, std::pair<unsigned int const, boost::shared_ptr<T const> const> >
{
std::pair<unsigned int const, boost::shared_ptr<T const> const> operator() (std::pair<int const, boost::shared_ptr<T> const> const & orig) const
{
std::pair<int const, boost::shared_ptr<T const> const> newPair(orig.first, boost::const_pointer_cast<T const>(orig.second));
return newPair;
}
};
这是“核心”错误:
注意:候选函数不可行: 'const 没有已知的转换 boost::bimaps::relation::structured_pair, boost::bimaps::tags::tagged, boost::bimaps::relation::member_at::right>, mpl_::na, boost::bimaps::relation::normal_layout>' 到 'const std::pair >' 为 第一个论点
【问题讨论】: