2010-06-05 63 views
0

对不起,混淆标题。让我通过代码解释:boost :: function&boost :: lambda - 调用网站调用和访问_1和_2作为类型

#include <string> 
#include <boost\function.hpp> 
#include <boost\lambda\lambda.hpp> 
#include <iostream> 

int main() 
{ 
    using namespace boost::lambda; 

    boost::function<std::string(std::string, std::string)> f = 
     _1.append(_2); 

    std::string s = f("Hello", "There"); 
    std::cout << s; 

    return 0; 
} 

我试图用function来创建使用labda表达式来创建一个新的返回值的函数,并调用该函数的调用点,s = f("Hello", "There");

当我编译,我得到:

1>------ Build started: Project: hacks, Configuration: Debug x64 ------ 
1>Compiling... 
1>main.cpp 
1>.\main.cpp(11) : error C2039: 'append' : is not a member of 'boost::lambda::lambda_functor<T>' 
1>  with 
1>  [ 
1>   T=boost::lambda::placeholder<1> 
1>  ] 

使用MSVC 9

我的基本认识210和lambda可能缺乏。今天上午的教程和文档没有帮助。

我该怎么做我想做的事?

回答

2

您需要:

boost::function<std::string(std::string, std::string)> f = 
    boost::bind(&std::string::append, _1, _2); 
+0

+1 Thanks 6more – 2010-06-05 19:10:57

1

我不会假装我明白boost.lambda,但以下似乎工作:

#include <string> 
#include <boost\function.hpp> 
#include <boost\lambda\lambda.hpp> 
#include <iostream> 

int main() 
{ 
    using namespace boost::lambda; 

    boost::function<std::string(std::string, std::string)> f = _1 + _2; 

    std::string s = f("Hello", "There"); 
    std::cout << s; 

    return 0; 
} 
+0

+1的确如此。让我问一个更好的问题。 – 2010-06-05 19:10:35

相关问题