をに変換する変換イテレータを作成できます。すでにBoostを使用しているので、次を使用できます。weak_ptr<T>
weak_ptr<const T>
boost::transform_iterator
#include <boost/iterator/transform_iterator.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <set>
// Functor to transform a weak_ptr<T> to a weak_ptr<const T>
template <typename T>
struct make_weak_ptr_const
: std::unary_function<boost::weak_ptr<T>, boost::weak_ptr<const T> >
{
boost::weak_ptr<const T> operator()(const boost::weak_ptr<T>& p) const
{
return p;
}
};
struct S { };
// Container demonstrating use of make_weak_ptr_const:
struct my_awesome_container
{
typedef std::set<boost::weak_ptr<S> > BaseSet;
typedef BaseSet::const_iterator BaseIterator;
typedef boost::transform_iterator<
make_weak_ptr_const<S>,
BaseIterator
> iterator;
iterator begin() const
{
return TransformedIterator(data.begin());
}
iterator end() const
{
return TransformedIterator(data.end());
}
std::set<boost::weak_ptr<S> > data;
};
を使用したくない場合boost::transform_iterator
は、独自に作成するのは簡単な作業です。別の質問への回答でこれを行う方法を示しました。