Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 246899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:12:39+00:00 2026-05-11T21:12:39+00:00

I want to create a function that can take different types of iterators which

  • 0

I want to create a function that can take different types of iterators which store the same type of object:
The first is a std::map containing shared_ptr<Foo> (typedef-ed as FooMap) and the other is a std::list which also contains shared_ptr<Foo> (FooList).

I really like the solution MSalters suggested for a similar question and tried to implement boost::variant iterators, which the function will get as parameters to iterate from the first to the second.

My function looks like this (simplified quite a bit):

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

The FooIterator and the 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; }
};

I got most of the above from the attachment of this mail. That solution also uses adaptors and policies, which seems to be a little too much, according to the answer of MSalters, so I don’t want to simply copy that code. Especially as I only understand part of it.

With the above code I get the following compiler error from VS2008 (this is only the first few lines of 160 in total, which I think is a bit too much to post here; however I’ll be happy to add them If somebody wants to see it all):

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

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:12:39+00:00Added an answer on May 11, 2026 at 9:12 pm

    I suspect you are missing cases on your do_compare static_visitor. Remeber, the variants might have anything, so you need all possible combinations, like comparing a FooList::const_iterator to a FooMap::const_iterator. It’s complaining because the compiler is trying to find some match for that case, and can’t convert a FooMap::const_iterator to a FooList::const_iterator.

    Hammering it out:

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

    Here’s a version with templates:

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

    It’s compiling on comeau, but I’m not 100% it will work, so some testing is required. Other than cleaner, more versatile code, it shouldn’t have any effect, as long as it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 206k
  • Answers 206k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Nope, the .net 3.5 run time environment can run along… May 12, 2026 at 9:09 pm
  • Editorial Team
    Editorial Team added an answer You could direct the path to a controller action and… May 12, 2026 at 9:09 pm
  • Editorial Team
    Editorial Team added an answer find /path/to/dir/ -type f -name "*.py" -exec md5sum {} +… May 12, 2026 at 9:09 pm

Related Questions

I am curious and hopefully someone can shed somelight on this - but why
I have an OLE COM object that trying to write a wrapper for, I
I'd like to know how do the member types work in Scala, and how
Okay this is a fairly broad question. This is my first App and I'm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.