我希望创建一个访问者工厂,它返回以下任一类型的对象:
class Visitor1: public boost::static_visitor<int>
{
public:
int operator()(int& ) const
{
return 1;
}
int operator()(bool& ) const
{
return 1;
}
};
class Visitor2: public boost::static_visitor<int>
{
public:
int operator()(int& ) const
{
return 2;
}
int operator()(bool& ) const
{
return 2;
}
};
我假设我可以为工厂创建的对象返回类型为boost :: static_visitor *的指针,但是这不会编译。我也无法参考该对象,如下所示:
Visitor1 v;
Type t;
boost::static_visitor<int>& vR = v;
boost::apply_visitor(vR, t);
我得到的错误是:
see reference to function template instantiation 'int boost::apply_visitor<boost::static_visitor<R>,Type>(Visitor &,Visitable &)' being compiled
1> with
1> [
1> R=int,
1> Visitor=boost::static_visitor<int>,
1> Visitable=Type
1> ]
1>c:\boost_1_49_0\boost\variant\variant.hpp(858): error C2064: term does not evaluate to a function taking 1 arguments
我应该如何实施我的工厂?
答案 0 :(得分:1)
访问者使用重载,这意味着他们是一个静态组件。工厂始终(?)表示运行时组件。首先,确定所有派生的访问者是否共享同一组重载是很重要的。如果是这样,您可以使用此方案:
#include <memory>
#include <boost/variant.hpp>
#include <boost/variant/static_visitor.hpp>
struct visitor_base : public boost::static_visitor<int>
{
virtual int operator()(int) = 0;
virtual int operator()(double) = 0;
virtual int operator()(const char*) = 0;
virtual ~visitor_base() {}
};
struct impl_1 : visitor_base {
virtual int operator()(int) { return 1; }
virtual int operator()(double) { return 1; }
virtual int operator()(const char*) { return 1; }
};
struct impl_2 : visitor_base {
virtual int operator()(int) { return 2; }
virtual int operator()(double) { return 2; }
virtual int operator()(const char*) { return 2; }
};
std::unique_ptr<visitor_base> visitor_factory(int i) {
if(i == 1) {
return std::unique_ptr<visitor_base>(new impl_1);
} else {
return std::unique_ptr<visitor_base>(new impl_2);
}
}
int main()
{
auto vis = visitor_factory(1);
boost::variant<int, double, const char*> v = 3;
std::cout << boost::apply_visitor(*vis, v) << std::endl;
auto vis2 = visitor_factory(2);
std::cout << boost::apply_visitor(*vis2, v) << std::endl;
return 0;
}
如果派生的访问者仅用于应用变体中可能类型的子集,则不能使用纯虚函数,并且需要一些报告失败的机制来处理基础中的值(例如异常)。
当然,示例代码中使用的一些C ++ 11特性很容易替换。
#include <memory>
#include <string>
#include <boost/variant.hpp>
#include <boost/variant/static_visitor.hpp>
struct visitor_base : public boost::static_visitor<int>
{
// the catch all
template <typename T>
int operator()(const T& t) {
return 0;
}
virtual int operator()(int) {}
virtual int operator()(double) {}
virtual int operator()(const char*) {}
virtual ~visitor_base() {}
};
struct impl_1 : visitor_base {
virtual int operator()(int) { return 1; }
virtual int operator()(double) { return 1; }
virtual int operator()(const char*) { return 1; }
};
struct impl_2 : visitor_base {
virtual int operator()(int) { return 2; }
virtual int operator()(double) { return 2; }
virtual int operator()(const char*) { return 2; }
};
std::unique_ptr<visitor_base> visitor_factory(int i) {
if(i == 1) {
return std::unique_ptr<visitor_base>(new impl_1);
} else {
return std::unique_ptr<visitor_base>(new impl_2);
}
}
int main()
{
auto vis = visitor_factory(1);
boost::variant<int, double, const char*> v = 3;
std::cout << boost::apply_visitor(*vis, v) << std::endl;
auto vis2 = visitor_factory(2);
std::cout << boost::apply_visitor(*vis2, v) << std::endl;
// a variant with more than impl_1 and impl_2 can catch
boost::variant<int, double, const char*, std::string> vv = std::string{"asdf"};
std::cout << boost::apply_visitor(*vis2, vv) << std::endl;
// no use one that we handle
vv = 3;
std::cout << boost::apply_visitor(*vis2, vv) << std::endl;
return 0;
}