代码之家  ›  专栏  ›  技术社区  ›  navigator

lambda bind表达式无法使bind to string的empty()工作

  •  1
  • navigator  · 技术社区  · 14 年前

    我正在尝试编译下面的代码片段。但它失败了:

    知道我做错了什么吗?

    #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;
    }
    
    2 回复  |  直到 10 年前
        1
  •  2
  •   Anycorn    14 年前

    小的改动使它可以用g++编译(是时候更好的编译器了-)?)

     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 }
    

    通知缺失包括和 bl::_1

        2
  •  1
  •   jfs    14 年前
    #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";
    ...