【问题标题】:Visitor pattern using boost::bind & overloaded functions使用 boost::bind 和重载函数的访问者模式
【发布时间】:2011-05-14 17:25:06
【问题描述】:

我正在尝试将访问者模式添加到我的代码中,并希望使其尽可能通用。更具体地说,我不想将回调函数硬编码到我的accept 函数中。所以,作为accept函数的参数,我给出了一个boost::function对象,然后被访问对象调用。

但是,我的问题是我无法绑定到重载函数(因为 boost::bind 不知道要绑定到哪个确切函数)并且我无法将重载函数强制转换为正确的函数,因为我不知道访问类的确切类型(这很重要)。

有什么方法可以创造我想要的东西吗?我搜索了 SO,但只发现有关如何解决绑定问题的问题(通过强制转换,这是我无法做到的)。

以下是一些无法编译的代码,但显示了我想要归档的内容:

#include <string>
#include <vector>
#include <boost/bind.hpp>
#include <boost/function.hpp>

struct A
{
    virtual void acceptVisitor (boost::function<void (A const &)> callbackFnc)
    {
        callbackFnc(*this);
    }
};

struct B : virtual public A {};

std::string printMe (A const & a) { return "A"; }
std::string printMe(B const & a) { return "B"; }


int main()
{
    std::vector<std::string> stringVector;

    boost::function<void (A const &)> bindedFnc = boost::bind(&std::vector<std::string>::push_back, 
        &stringVector, boost::bind(&printMe, _1));

    A A1;
    B A2;

    A1.acceptVisitor(bindedFnc);
    A2.acceptVisitor(bindedFnc);
}

[编辑] 修正了示例代码,因为以前的版本(如 ildjarn 所说)实际上并未调用 accept 函数。

【问题讨论】:

  • 请注意,您也不能获取push_back 的地址,因为它可能被重载(在 C++0x 中,它保证被重载)并且您不能使用强制转换,因为未指定标准库成员函数的类型(实现可以随意向其成员函数添加额外的重载和/或可选参数)。
  • "下面是一些无法编译的代码,但显示了我想要归档的内容" 不,它没有; acceptVisitor 在哪里发挥作用?实际访问在哪里?尝试描述你想要什么,因为你还没有有效地展示它。
  • @ildjarn 好点,固定示例代码。

标签: c++ boost visitor-pattern


【解决方案1】:

这应该能让你成功。它使用 Boost 1.46.0 与 Visual C++ 2010 和 g++ 4.5.1 一起编译。它不能与 Visual C++ 2010 C++0x &lt;functional&gt; 实现一起编译;我还不知道为什么。

设置:

#include <iostream>
#include <iterator>
#include <string>
#include <vector>

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

// This helper allows you to do a push_back in a bind; you can't bind
// directly to std::vector::push_back because the type of a Standard
// Library member function is unspecified.
struct do_push_back
{
    typedef void result_type;

    template <typename TSequence, typename TElement>
    void operator()(TSequence* sequence, const TElement& element) const
    {
        sequence->push_back(element);
    }
};

演示:

// Class hierarchy for demonstration:
struct B { };
struct D : B { };

// Instead of using overlodaed nonmember functions, you can overload
// operator() in a function object.  This allows you to bind to an 
// instance of this function object, not directly to one of the overloads.
struct make_string
{
    typedef std::string result_type;
    std::string operator()(const B&) const { return "B"; }
    std::string operator()(const D&) const { return "D"; }
};

int main()
{
    std::vector<std::string> strings;

    // Note that we do not use a boost::function here:
    auto f = boost::bind(do_push_back(), 
                         &strings, 
                         boost::bind(make_string(), _1));

    // Call our 'f' with B and D objects:
    f(B());
    f(D());

    std::copy(strings.begin(), strings.end(),
              std::ostream_iterator<std::string>(std::cout));
}

结果:

BD

这就是为什么这只是解决方案的一半:您不能将调用boost::bind 的结果存储在boost::function 中。问题是当您使用boost::function&lt;void(const B&amp;)&gt; 来存储绑定函数对象时,它总是将const A&amp; 作为参数传递给绑定函数。

即使您使用D 参数调用boost::function 对象,它也会转换为const B&amp;。使用boost::function 时会丢失很多类型信息;这种类型信息的丢失对于使boost::function 可用作通用可调用对象容器是必要的。

但这并不意味着您不能传递绑定的函数对象;你只需要使用模板来防止类型信息丢失:

template <typename TFunction>
void test(std::vector<std::string>& strings, TFunction f)
{
    f(B());
    f(D());
}

// In main():
test(strings, f);

// Or, if you don't have C++0x's "auto", you can pass the bound 
// function object directly:
test(strings, boost::bind(do_push_back(), 
                          &strings, 
                          boost::bind(make_string(), _1)));

不幸的是,为了不丢失类型信息,您必须将绑定的函数对象传递给函数模板。这意味着您将acceptVisitor 设为虚成员函数的想法不适用于此解决方案(不可能有虚函数模板)。

无论如何,希望这对您有所帮助。

【讨论】:

  • @james-mcnellis 感谢您的明确解释。很遗憾,我想要的东西在 C++ 中似乎是不可能的。我想知道这种事情通常是如何实现的?是否仅修复“回调”功能?或者还有其他一些我不知道的技巧可以使用吗?
【解决方案2】:

也许您会发现 Loki 库中的通用访问者模板很有用

http://loki-lib.sourceforge.net

#include <iostream>
#include <Loki/Visitor.h>

struct Animal : public Loki::BaseVisitable<void, Loki::DefaultCatchAll, false>
{
};

struct Cat : public Animal
{
    LOKI_DEFINE_VISITABLE();
};

struct Dog : public Animal
{
    LOKI_DEFINE_VISITABLE();
};

struct Flower : public Loki::BaseVisitable<void, Loki::DefaultCatchAll, false>
{
};

struct Tulip : public Flower
{
    LOKI_DEFINE_VISITABLE();
};

struct AnimalAndFlowerVisitor 
    : public Loki::BaseVisitor
    , public Loki::Visitor<Cat, void, false>
    , public Loki::Visitor<Dog, void, false>
    , public Loki::Visitor<Tulip, void, false>
{
    void Visit(Dog & dog)
    {
        std::cout << "Do something with the dog\n";
    }

    void Visit(Cat & cat)
    {
        std::cout << "Do something with the cat\n";
    }

    void Visit(Tulip & tulip)
    {
        std::cout << "Do something with the tulip\n";
    }
};

int main(int argc, char* argv[])
{
    Dog dog;
    Cat cat;
    Tulip tulip;

    Animal & animalDog = dog;
    Flower & tulipFlower = tulip;

    AnimalAndFlowerVisitor visitor;
    animalDog.Accept(visitor);    // will print "Do something with the dog"
    tulipFlower.Accept(visitor);  // will print "Do something with the tulip"

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 2012-04-18
    • 2010-12-27
    相关资源
    最近更新 更多