Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost::bind and std::copy

Tags:

c++

bind

boost

I'm trying to use Boost::bind and std::copy to print out the values in a list of lists. Obviously, I could use loops, and I may end up doing so for clarity, but I'd still like to know what I'm doing wrong here.

Here is the distilled version of my code:

#include <boost/bind.hpp>
#include <iterator>
#include <algorithm>
#include <list>
#include <iostream>
using namespace std;
using namespace boost;

int main(int argc, char **argv){
list<int> a;
a.push_back(1);

list< list<int> > a_list;
a_list.push_back(a);

ostream_iterator<int> int_output(cout,"\n");

for_each(a_list.begin(),a_list.end(),
  bind(copy,
    bind<list<int>::iterator>(&list<int>::begin,_1),
    bind<list<int>::iterator>(&list<int>::end,_1),
    ref(int_output)
  ) //compiler error at this line
);
return 0;

}

The compiler error starts off

error: no matching function call to bind(<unresolved overloaded function type> .....

I think this means that bind can't figure out what the return type for the outermost bind should be. I don't blame it, because I can't either. Any ideas?

like image 833
Dan Hook Avatar asked Apr 18 '26 01:04

Dan Hook


1 Answers

The template arguments to std::copy cannot deduced in the context of the bind call. You need to specify them explicitly:

copy< list<int>::iterator, ostream_iterator<int> >

Also when you write:

for_each(a_list.begin().a_list.end(),

I think that you mean:

for_each(a_list.begin(),a_list.end(),

And you're missing #include <iostream> for definition of std::cout.

like image 131
CB Bailey Avatar answered Apr 20 '26 16:04

CB Bailey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!