1

I am trying to get the below code snippet to compile. But it fails with:

error C2665: 'boost::lambda::function_adaptor::apply' : none of the 8 overloads could convert all the argument types. Specifying the return type when calling bind does not help.

Any idea what I am doing wrong?

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <string>
#include <map>

int main()
{

    namespace bl = boost::lambda;
    typedef std::map<int, std::string> types;
    types keys_and_values;
    keys_and_values[ 0 ] = "zero";
    keys_and_values[ 1 ] = "one";
    keys_and_values[ 2 ] = "Two";

    std::for_each(
        keys_and_values.begin(),
        keys_and_values.end(),
        std::cout <<  bl::constant("Value empty?: ") << std::boolalpha <<  
                        bl::bind(&std::string::empty,
                                bl::bind(&types::value_type::second, _1)) << "\n");




    return 0;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
navigator
  • 1,588
  • 1
  • 13
  • 19

2 Answers2

2

minor changes to make it compile with g++ (time for better compiler :-) ?)

 1 #include <boost/lambda/lambda.hpp>
 2 #include <boost/lambda/bind.hpp>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <string>
 6 #include <map>
 7
 8 int main()
 9 {
10
11     namespace bl = boost::lambda;
12     typedef std::map<int, std::string> types;
13     types keys_and_values;
14     keys_and_values[ 0 ] = "zero";
15     keys_and_values[ 1 ] = "one";
16     keys_and_values[ 2 ] = "Two";
17
18     std::for_each(
19                   keys_and_values.begin(),
20                   keys_and_values.end(),
21                   std::cout <<  bl::constant("Value empty?: ") << std::boolalpha <<
22                   bl::bind(&std::string::empty,
23                            bl::bind(&types::value_type::second, bl::_1)) << "\n");
24
25
26
27
28     return 0;
29 }

notice missing includes and bl::_1

Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • ahhh thanks. Found out that boot/bind.hpp was getting included in my project. Once I removed the include, it worked fine. Don't know why it did not give me a more descriptive error. – navigator May 08 '10 at 17:08
  • @navigator yes, I ran into that myself. boost bind puts _1 in anonymous namespace which causes all kind of confusion with lambda – Anycorn May 08 '10 at 17:10
1
#include <boost/foreach.hpp>
...
#define foreach BOOST_FOREACH
...
foreach(types::value_type kv, keys_and_values)
  std::cout <<  "Value empty?: " << std::boolalpha 
            <<  kv.second.empty() << "\n";
...
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • A vastly superior approach to trying to trick `std::for_each` to do anything beyond trivial! Range-based for loops are also there in C++0x. – UncleBens May 08 '10 at 18:00
  • I am using for_each in this example just to learn about boost::lambda bind capabilities. – navigator May 08 '10 at 18:36
  • @navigator: Ok then. I still think boost.lambda will also be made obsolete by C++0x. – UncleBens May 08 '10 at 19:42
  • 1
    @UncleBens I think i read that phoenix3 will replace boost.lambda. It still has its place since c++0x lambdas are only monomorphic. Phoenix3 and boost.lambda are fully polymorphic. SO you can pass `_1 << 2` and have a fully generic shift-expression. – Johannes Schaub - litb May 09 '10 at 19:18