«Невозможно преобразовать параметр» с помощью итератора boost::variant
-
13-09-2019 - |
Вопрос
Я хочу создать функцию, которая может принимать итераторы разных типов, хранящие объекты одного и того же типа:
Первый - это std::map
содержащий shared_ptr<Foo>
(определено по типу как FooMap
), а другой - std::list
который также содержит shared_ptr<Foo>
(FooList
).
мне действительно нравится решение, предложенное MSalters для аналогичного вопроса и попробовал реализовать boost::variant
итераторы, которые функция получит в качестве параметров для перебора от первого ко второму.
Моя функция выглядит так (немного упрощенно):
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; }
};
у меня есть большая часть из вышеперечисленного из вложения этого письма.В этом решении также используются адаптеры и политики, что, согласно ответу 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> ]
[...]
Что я делаю не так?
Решение
Я подозреваю, что в вашем do_compare static_visitor отсутствуют случаи.Помните, варианты могут иметь что угодно, поэтому вам нужны все возможные комбинации, например сравнение FooList::const_iterator с FooMap::const_iterator.Он жалуется, потому что компилятор пытается найти совпадение для этого случая и не может преобразовать FooMap::const_iterator в FooList::const_iterator.
Выбивая это:
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; }
bool operator() (
const FooMap::const_iterator & a,
const FooList::const_iterator & b) const
{ return false; }
bool operator() (
const FooList::const_iterator & a,
const FooMap::const_iterator & b) const
{ return false; }
};
Вот версия с шаблонами:
template <typename A, typename B>
bool operator() (
const A & a,
const B & b) const
{ return false; }
template <typename A>
bool operator() (
const A & a,
const A & b) const
{ return a != b; }
Он компилируется на Comeau, но я не уверен, что он будет работать на 100%, поэтому требуется некоторое тестирование.Кроме более чистого и универсального кода, он не должен иметь никакого эффекта, пока работает.