I want to create a function that can accept different types of iterators that store the same type of object:
The first is one std::mapthat contains shared_ptr<Foo>(typedef-ed as FooMap), and the other one std::listthat also contains shared_ptr<Foo>( FooList).
I really like the MSalters solution proposed for a similar question , and tried to implement iterators boost::variant, which the function will receive as parameters for iterating from the first to the second.
My function looks like this (completely simplified):
set<Foo> CMyClass::GetUniqueFoos(FooIterator itBegin, FooIterator itEnd)
{
set<Foo> uniques;
for(;
apply_visitor(do_compare(), itBegin, itEnd);
apply_visitor(do_increment(), itBegin))
{
uniques.insert( do_dereference< shared_ptr<Foo> >(), itBegin) );
}
return uniques;
}
FooIterator and visitors are defined as follows:
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; }
};
. , , , , MSalters, . , .
VS2008 ( 160, , , , If - ):
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> ]
[...]
?