2009-06-09 4 views
3

를 사용하여 "매개 변수를 변환 할 수 없습니다")이고, 다른 하나는 std::list이고, 또한 shared_ptr<Foo> (FooList)을 포함한다.내가 동일한 유형의 개체를 저장 반복자의 다른 유형을 취할 수있는 함수를 만들려면 부스트 :: 변형 반복자

나는 the solution MSalters suggested for a similar question을 정말로 좋아하고, 함수는 반복자를 구현하려고 시도했다.이 반복자는 함수가 첫 번째에서 두 번째까지 반복하는 매개 변수로 가져옵니다.

내 기능 (간체 꽤) 다음과 같습니다 : 다음과 같이

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>  ] 
[...] 

내가 뭘 잘못하고 있니?

답변

6

do_compare static_visitor에서 케이스가 누락 된 것으로 의심됩니다. Remeber에는 변형이있을 수 있으므로 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; } 
}; 

여기 템플릿 버전 : 그것은 꼬모에서 컴파일,하지만 나는 100 % 아니에요

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; } 

그렇게 몇 가지 테스트가 필요하며, 작동합니다. 보다 깔끔하고 다재다능한 코드를 제외하고는 작동하는 한 효과가 없어야합니다.

+0

위대한 작품입니다! 감사! 왜 내가 템플릿을 사용하여 댓글을 삭제했는지 모르겠다. 그들을 사용하는 부작용이 있습니까? – foraidt

관련 문제