tags:

views:

44

answers:

2

Sorry for the confusing title. Let me explain via code:

#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;
}

I'm trying to use function to create a function that uses the labda expressions to create a new return value, and invoke that function at the call site, s = f("Hello", "There");

When I compile this, I get:

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>        ]

Using MSVC 9.

My fundamental understanding of function and lambdas may be lacking. The tutorials and docs did not help so far this morning.

How do I do what I'm trying to do?

+1  A: 

I'm not going to pretend that I understand boost.lambda, but the following seems to work:

#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;
}
Ferruccio
+1 True enough. Let me ask a better question.
John Dibling
+2  A: 

You need:

boost::function<std::string(std::string, std::string)> f =
    boost::bind(&std::string::append, _1, _2);
rlbond
+1 Thanks 6more
John Dibling