我正在尝试将访问者模式添加到我的代码中,并希望尽可能保持通用。更具体地说,我不想将回调函数硬编码到我的 accept
函数中。因此,作为 accept
函数的参数,我给出了一个 boost::function
对象,然后由访问的对象调用它。
然而,我的问题是我无法绑定(bind)到重载函数(因为 boost::bind 不知道要绑定(bind)到哪个确切的函数)并且我无法将重载函数转换为正确的函数,因为我不知道访问类的确切类型(这很重要)。
有什么方法可以创造我想要的东西吗?我搜索了 SO,但只发现了有关如何解决绑定(bind)问题的问题(这是通过转换,这是我做不到的)。
下面是一些无法编译的代码,但显示了我想要实现的目标:
#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
函数。
最佳答案
这应该让你成功了一半。它使用 Visual C++ 2010 和 g++ 4.5.1 使用 Boost 1.46.0 进行编译。它不使用 Visual C++ 2010 C++0x <functional>
编译执行;我还不确定为什么。
设置:
#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<void(const B&)>
存储绑定(bind)函数对象,它总是传递一个 const A&
作为绑定(bind)函数的参数。
即使您调用 boost::function
带有 D
的对象参数,它被转换为 const B&
.使用 boost::function
时会丢失很多类型信息;这种类型信息的丢失是制作 boost::function
所必需的可用作通用的可调用对象容器。
但这并不意味着您不能传递绑定(bind)的函数对象;你只需要使用模板来防止类型信息丢失:
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)));
不幸的是,为了不丢失类型信息,您必须将绑定(bind)的函数对象传递给函数模板。这意味着你制作 acceptVisitor
的想法虚拟成员函数不适用于此解决方案(不可能有虚拟函数模板)。
无论如何,希望这对您有所帮助。
关于c++ - 使用 boost::bind 和重载函数的访问者模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6003561/