Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
c++ lambda without using C++11 [solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Wed Jan 09, 2013 7:53 am    Post subject: c++ lambda without using C++11 [solved] Reply with quote

Hello,

I'm trying write a lambda example program to try and understand it more.
I have the following code:
Code:

#include <boost/lambda/bind.hpp>
#include <iostream>
#include <vector>

template< typename Iterator, typename Callback >
void print1(Iterator start, Iterator end, Callback func)
{
  for (Iterator it = start; it != end; it++)
     func(it);
}

template< typename Iterator >
void print_it(Iterator it)
{
   std::cout << *it << std::endl;
}

int main()
{
  std::vector< int > v;
  for( int i = 1; i < 6; i++)
    v.push_back(i);

  print1< std::vector< int >::iterator >(v.begin(), v.end(), boost::lambda::bind(&print_it, boost::lambda::_1));
  return 0;
}


when I compile it, I get this:
Code:

bltf.cpp: In function ‘int main()’:
bltf.cpp:110:110: error: no matching function for call to ‘bind(<unresolved overloaded function type>, boost::lambda::placeholder1_type&)’
bltf.cpp:110:110: note: candidates are:

as I'm touching lambda for the first time, I'm having hard time to understand what is messing or what I did wrong, can anyone assist me on this matter?

btw, I cannot use C++11 or similar official extensions, I can however use boost.

Thanks.
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein


Last edited by DaggyStyle on Wed Jan 09, 2013 8:13 am; edited 1 time in total
Back to top
View user's profile Send private message
aderesch
Tux's lil' helper
Tux's lil' helper


Joined: 06 Mar 2010
Posts: 123
Location: Hamburg, Germany

PostPosted: Wed Jan 09, 2013 8:03 am    Post subject: Re: c++ lambda without using C++11 Reply with quote

Just looking at it, I'd say you need to instantiate the print_it template for the bind call:
Code:
print_it< std::vector< int >::iterator >


ad
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Wed Jan 09, 2013 8:12 am    Post subject: Reply with quote

right! it worked!

the only replacement I needed is this:
Code:
print1< std::vector< int >::iterator >(v.begin(), v.end(), boost::lambda::bind(&print_it, boost::lambda::_1));

to this:
Code:
print1< std::vector< int >::iterator >(v.begin(), v.end(), boost::lambda::bind(&print_it< std::vector< int >::iterator >, boost::lambda::_1));


I've should have seen it, thanks!
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21431

PostPosted: Thu Jan 10, 2013 2:11 am    Post subject: Reply with quote

To clarify, the problem, as reported in the original error message, is that the bare name print_it is ambiguous. If you were calling it at the place it was named, the compiler could use the parameter list to disambiguate. With no parameter list, the function declared as a template, and boost::bind overloaded to accept a variety of function signatures, the compiler is unable to determine the correct type to bind to your typename Iterator. A non-template version of print_it would likely be unambiguous even with a bare name.
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Thu Jan 10, 2013 6:17 am    Post subject: Reply with quote

Hu wrote:
To clarify, the problem, as reported in the original error message, is that the bare name print_it is ambiguous. If you were calling it at the place it was named, the compiler could use the parameter list to disambiguate. With no parameter list, the function declared as a template, and boost::bind overloaded to accept a variety of function signatures, the compiler is unable to determine the correct type to bind to your typename Iterator. A non-template version of print_it would likely be unambiguous even with a bare name.


thanks for clearing it.
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum