を使用して「パラメータを変換できません」 )であり、他方はstd::list
であり、shared_ptr<Foo>
(FooList
)も含有する。は、私は同じ種類のオブジェクトを保存するイテレータの種類を取ることができます関数を作成したいブースト::バリアントイテレータ
私は実際にthe solution MSalters suggested for a similar questionが好きで、boost::variant
イテレータを実装しようとしました。この関数は、最初から2番目の繰り返しにパラメータとして取得されます。
My機能は、この(かなり単純化)のようになります。
set<Foo> CMyClass::GetUniqueFoos(FooIterator itBegin, FooIterator itEnd)
{
set<Foo> uniques;
for(/**/;
apply_visitor(do_compare(), itBegin, itEnd); // equals "itBegin != itEnd"
apply_visitor(do_increment(), itBegin)) // equals "++itBegin"
{
// Exact mechanism for determining if unique is omitted for clarity
uniques.insert(do_dereference< shared_ptr<Foo> >(), itBegin));
}
return uniques;
}
次のようにFooIterator、訪問者が定義されています
typedef
boost::variant<
FooMap::const_iterator,
FooList::const_iterator>
FooIterator;
struct do_compare : boost::static_visitor<bool>
{
bool operator() (
const FooMap::const_iterator & a,
const FooMap::const_iterator & b) const
{ return a != b; }
bool operator() (
const FooList::const_iterator & a,
const FooList::const_iterator & b) const
{ return a != b; }
};
struct do_increment: boost::static_visitor<void>
{
template<typename T>
void operator()(T& t) const
{ ++t; }
};
template< typename Reference >
struct do_dereference: boost::static_visitor<Reference>
{
template<typename T>
Reference operator()(const T& t) const
{ return *t; }
};
私は上記from the attachment of this mailのほとんどを得ました。その解決策は、MSaltersの答えによると、あまりにも多すぎるように見えるアダプタとポリシーも使用しているので、単にそのコードをコピーするだけではありません。特に私はその一部だけを理解しています。
上記のコードでは、VS2008から次のコンパイラエラーが発生します(これは合計で160行の最初の数行ですが、ここに投稿するにはあまりにも多すぎると思いますが、誰かがそれをすべて見たいと思っている場合):
1>c:\boost\boost\variant\detail\apply_visitor_binary.hpp(63) :
error C2664: 'bool CMyClass::do_compare::operator()(
const std::list<_Ty>::_Const_iterator<_Secure_validation> &,
const std::list<_Ty>::_Const_iterator<_Secure_validation> &) const' :
cannot convert parameter 1 from 'T0' to
'const std::list<_Ty>::_Const_iterator<_Secure_validation> &'
1> with
1> [
1> _Ty=shared_ptr<Foo>,
1> _Secure_validation=true
1> ]
1> Reason: cannot convert from 'T0' to 'const std::list<_Ty>::_Const_iterator<_Secure_validation>'
1> with
1> [
1> _Ty=shared_ptr<Foo>,
1> _Secure_validation=true
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\boost\boost\variant\variant.hpp(806) : see reference to function template instantiation 'bool boost::detail::variant::apply_visitor_binary_invoke<Visitor,Value1>::operator()<T>(Value2 &)' being compiled
1> with
1> [
1> Visitor=const CMyClass::do_compare,
1> Value1=T0,
1> T=T1,
1> Value2=T1
1> ]
[...]
私は間違っていますか?
素晴らしいです。ありがとう! なぜ私はあなたがテンプレートでコメントを削除したのか分かりません。それらの使用に副作用はありますか? – foraidt