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 8619599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:22:46+00:00 2026-06-12T06:22:46+00:00

I’m somewhat trying to implement a kind of copy operator. The aim is to

  • 0

I’m somewhat trying to implement a kind of copy operator.
The aim is to have two classes: one that browse a container, and one that do something with it. The Browse class also maintain (for some reason) an iterator on the ouput container, and the other one can compute an increment with it.

Unfortunately, the compiler seems to be unable to convert a back_insert_iterator to the output iterator. Why?

#include <iostream>
#include <iterator>
#include <vector>

typedef std::vector<int> Vec;

// An operator that copy an item onto another
template< class TIN, class TOUT >
class DoCopy
{
    TOUT operator()( const typename TIN::iterator i_in, const typename TOUT::iterator i_out )
    {
        const typename TOUT::iterator i_incr = i_out;
        (*i_incr) = (*i_in);
        std::advance( i_incr, 1 );
        return i_incr;
    }
};

// The class that iterate over a container, calling an operator for each item
template< class TIN, class TOUT >
class Browse
{
    public:
        // We keep a reference to the operator that really do the job
        DoCopy<TIN,TOUT> & _do;
        Browse( DoCopy<TIN,TOUT> & op ) : _do(op) {}

        // Iterate over an input container
        TOUT operator()(
                const typename TIN::iterator in_start,
                const typename TIN::iterator in_end,
                const typename TOUT::iterator out_start
            )
        {
            TOUT i_out = out_start;

            for( TIN i_in = in_start; i_in != in_end; ++i_in ) {
                // it is not shown why here, but we DO want the operator to increment i_out
                i_out = _do(i_in, i_out);
            }
        }
};

int main()
{
    // in & out could be the same type or a different one
    Vec in;
    Vec out;
    DoCopy<Vec,Vec> do_copy;
    Browse<Vec,Vec> copy(do_copy);

    std::back_insert_iterator< Vec > insert_back(out);

    // Here, g++ cannot find the corresponding function :
    copy( in.begin(), in.end(), insert_back );

}

g++ fail to compile with the following errors:

$ g++ test.cpp && ./a.out
    test.cpp: In function ‘int main()’:
    test.cpp:54:49: erreur: no match for call to ‘(Browse<std::vector<int>, std::vector<int> >) (std::vector<int>::iterator, std::vector<int>::iterator, std::back_insert_iterator<std::vector<int> >&)’
    test.cpp:22:11: note: candidate is:
    test.cpp:30:18: note: TOUT Browse<TIN, TOUT>::operator()(typename TIN::iterator, typename TIN::iterator, typename TOUT::iterator) [with TIN = std::vector<int>, TOUT = std::vector<int>, typename TIN::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >, typename TOUT::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]
    test.cpp:30:18: note:   no known conversion for argument 3 from ‘std::back_insert_iterator<std::vector<int> >’ to ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’
  • 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-06-12T06:22:47+00:00Added an answer on June 12, 2026 at 6:22 am

    Here is the main source of the problem: std::back_insert_iterator< V<T> > and std::vector<T>::iterator aren’t directly related in their inheritance tree:

    • std::vector<T>::iterator is a __normal_iterator<T> (no other super class) (look at stl_vector.h for std::vector<T>::iterator and stl_iterator.h for __normal_iterator)
    • std::back_insert_iterator< V<T> > is an iterator (no other super class) (look at stl_iterator.h for std::back_insert_iterator and stl_iterator_base_types.h for std::iterator).

    They can’t be converted in any direction.

    Hence, the second template argument should directly be the std::back_insert_iterator or the iterator<> with the good first parameter indicating that’s an output operator.

    By std::advance( iterator, 1 ), I assume you meant ++iterator, which is the standard way to go to the next element for iterators.

    Furthermore, out iterators shouldn’t be const, otherwise they don’t implement the affectation operator=.

    Line 38, the i_in should be of type typename TIN::iterator and not TIN.
    The Browse operator() must also return the out iterator.

    The final code looks like this:

    #include <iostream>
    #include <iterator>
    #include <vector>
    
    typedef std::vector<int> Vec;
    
    // An operator that copy an item onto another
    template< class TIN, class TOUT >
    class DoCopy
    {
        public:
    
        TOUT operator()( const typename TIN::iterator i_in, const TOUT i_out )
        {
            TOUT i_incr = i_out;
            (*i_incr) = (*i_in);
            //std::advance( i_incr, 1 );
            ++i_incr;
            return i_incr;
        }
    };
    
    // The class that iterate over a container, calling an operator for each item
    template< class TIN, class TOUT >
    class Browse
    {
        public:
            // We keep a reference to the operator that really do the job
            DoCopy<TIN,TOUT> & _do;
            Browse( DoCopy<TIN,TOUT> & op ) : _do(op) {}
    
            // Iterate over an input container
            TOUT operator()(
                    const typename TIN::iterator in_start,
                    const typename TIN::iterator in_end,
                    const TOUT out_start
                )
            {
                TOUT i_out = out_start;
    
                for( typename TIN::iterator i_in = in_start; i_in != in_end; ++i_in ) {
                    // it is not shown why here, but we DO want the operator to increment i_out
                    i_out = _do(i_in, i_out);
                }
                return i_out;
            }
    };
    
    int main()
    {
        // in & out could be the same type or a different one
        Vec in;
    
        in.push_back(1);
        in.push_back(3);
        in.push_back(3);
        in.push_back(7);
    
        Vec out;
        DoCopy<Vec, std::back_insert_iterator<Vec> > do_copy;
        Browse<Vec, std::back_insert_iterator<Vec> > copy(do_copy);
    
        std::back_insert_iterator< Vec > insert_back(out);
    
        // Here, g++ cannot find the corresponding function :
        copy( in.begin(), in.end(), insert_back );
    
        for( unsigned i = 0, s = out.size(); i < s; ++i )
        {
            std::cout << out[i] << " ";
        }
        std::cout << std::endl;
    }
    

    Thanks to clang++ which makes C++ errors more clear.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.