3

我尝试学习 boost lambda 表达式,但这是行不通的。

如何在 for_each 中运行 Holder 的选定成员?

#include <iostream>
#include <string>

#include <boost/assign.hpp> 
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

using namespace std;
using namespace boost::assign;
using namespace boost::lambda;

class Holder
{
public:
    void vRun1(std::string s){ cout << "vRun1 " << s << endl; }
    void vRun2(std::string s){ cout << "vRun2 " << s << endl; }
    void vRun3(std::string s){ cout << "vRun3 " << s << endl; }
};

// --------------------
std::map< std::string, mem_fun_ref_t<void, Holder> > replacer;


insert(replacer)
        ("buh", std::mem_fun_ref(&Holder::vRun1))
        ("mar", std::mem_fun_ref(&Holder::vRun2))
        ("heu", std::mem_fun_ref(&Holder::vRun3));


Holder hol;

我如何在这里调用我在 中注册的函数map<>

for_each(replacer.begin(), replacer.end(), /* bind(_1, hol, it.first) */ );

结果应该是

vRun1 buh
vRun2 mar
vRun3 heu
4

2 回答 2

5

看起来好像您稍微复杂化了它。我会这样使用boost::function<>

在http://liveworkspace.org/code/b2c5a38d8c3499eefb6330a839a89d0a上现场观看

#define BOOST_RESULT_OF_USE_DECLTYPE
#include <iostream>
#include <string>
#include <map>
#include <boost/function.hpp>
#include <boost/foreach.hpp>
#include <boost/phoenix.hpp>
using namespace std;

class Holder {
public:
    void vRun1(std::string s){ cout << "vRun1 " << s << endl; }
    void vRun2(std::string s){ cout << "vRun2 " << s << endl; }
    void vRun3(std::string s){ cout << "vRun3 " << s << endl; }
};

typedef std::map< std::string, boost::function<void(Holder&, std::string)> > Replacer;

int main()
{
    Replacer replacer;
    replacer["a"] = &Holder::vRun1;
    replacer["b"] = &Holder::vRun2;
    replacer["c"] = &Holder::vRun3;

    Holder hol;

    for (Replacer::const_iterator it=replacer.begin(); it != replacer.end(); ++it)
    {
        (it->second)(hol, it->first);
    }

或者:

    std::cout << "Using BOOST_FOREACH:\n";

    BOOST_FOREACH(Replacer::value_type& pair, replacer)
    {
        (pair.second)(hol, pair.first);
    }

甚至:

    std::cout << "Using Boost Phoenix:\n";

    namespace phx = boost::phoenix;
    using namespace phx::arg_names;

    std::for_each(replacer.begin(), replacer.end(),
            phx::bind(
                phx::bind(&Replacer::value_type::second, arg1), 
                phx::ref(hol), 
                phx::bind(&Replacer::value_type::first, arg1)));
}

输出

vRun1 a
vRun2 b
vRun3 c
Using BOOST_FOREACH:
vRun1 a
vRun2 b
vRun3 c
Using Boost Phoenix:
vRun1 a
vRun2 b
vRun3 c
于 2012-10-12T14:28:58.897 回答
1

这对我有用:

#include <iostream>
#include <string>

#include <boost/assign.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/function.hpp>

using namespace boost::lambda;

class Holder
{
public:
    void vRun1(std::string s){ std::cout << "vRun1 " << s << std::endl; }
    void vRun2(std::string s){ std::cout << "vRun2 " << s << std::endl; }
    void vRun3(std::string s){ std::cout << "vRun3 " << s << std::endl; }
};

// --------------------
typedef std::map <std::string, 
                  boost::function<void(Holder&, std::string)> > Replacer_t;
Replacer_t replacer;
typedef Replacer_t::value_type ReplacerValue_t;

int main()
{
    boost::assign::insert(replacer)
        (std::string("buh"), &Holder::vRun1)
        (std::string("mar"), &Holder::vRun2)
        (std::string("heu"), &Holder::vRun3);


    Holder hol;

    for_each(replacer.begin(),
             replacer.end(),
             bind(protect(bind(bind(&ReplacerValue_t::second,_2),
                               _1,
                               bind(&ReplacerValue_t::first,_2))),
                  boost::ref(hol), _1));
}

与 boost::lambda 进行了大约 2 小时的战斗。在 C++11 中,我会在 30 秒内编写它,因此请相应地更新您的编译器。

于 2012-10-12T17:58:15.723 回答