boost::variant 的访问者模板
我想使用 boost.variant
void operator()(T0 value);
void operator()(T1 value);
void operator()(T2 value);
模板还将为变体中的每个类型 T0... 提供一个相应的虚拟函数,默认情况下该函数不执行任何操作。用户可以继承模板类并仅重新定义他感兴趣的虚函数。这类似于众所周知的“模板方法”模式。 我能想到的唯一解决方案是将 boost::variant 和关联的访问者包装在一个模板中,并通过 typedef 访问它们。这工作正常,但感觉有点笨拙。代码如下:
#include "boost/variant.hpp"
//create specializations of VariantWrapper for different numbers of variants -
//just show a template for a variant with three types here.
//variadic template parameter list would be even better!
template<typename T0, typename T1, typename T2>
struct VariantWrapper
{
//the type for the variant
typedef boost::variant<T0,T1,T2> VariantType;
//The visitor class for this variant
struct Visitor : public boost::static_visitor<>
{
void operator()(T0 value)
{
Process(value);
}
void operator()(T1 value)
{
Process(value);
}
void operator()(T2 value)
{
Process(value);
}
virtual void Process(T0 val){/*do nothing */}
virtual void Process(T1 val){/*do nothing */}
virtual void Process(T2 val){/*do nothing */}
protected:
Visitor(){}
};
typedef Visitor VisitorType;
private:
VariantWrapper(){}
};
然后按如下方式使用该类:
typedef VariantWapper<bool,int,double> VariantWrapperType;
typedef VariantWrapperType::VariantType VariantType;
typedef VariantWrapperType::VisitorType VisitorType;
struct Visitor : public VisitorType
{
void Process(bool val){/*do something*/}
void Process(int val){/*do something*/}
/* this class is not interested in the double value */
};
VariantType data(true);
apply_visitor(Visitor(),data);
正如我所说,这似乎工作正常,但如果我不必创建一个特殊的包装类来将变体和访问者联系在一起,我会更喜欢它。我希望能够直接使用 boost.variant 来实例化模板访问者类。我已经研究过使用类型参数、非类型参数和模板模板参数,但似乎没有任何提示。我想做的事情是不可能的吗?我可能会遗漏一些东西,如果有人对此有任何意见,我将不胜感激。
I would like to use a boost.variant<T0,T1,T2>
as a parameter to a template 'Visitor' class which would provide visitor operators as required by the boost.variant visitor mechanism, in this case all returning void i.e.,
void operator()(T0 value);
void operator()(T1 value);
void operator()(T2 value);
The template would also have for each of the types T0... in the variant a corresponding virtual function which by default does nothing. The user is able inherit from the template class and redefine only those virtual functions which he is interested in. This is something akin to the well-known 'Template Method' pattern.
The only solution I have been able to come up with is by wrapping both the boost::variant and the associated visitor in a single template, and accessing them via typedefs. This works okay, however it feels a little clunky. Here's the code:
#include "boost/variant.hpp"
//create specializations of VariantWrapper for different numbers of variants -
//just show a template for a variant with three types here.
//variadic template parameter list would be even better!
template<typename T0, typename T1, typename T2>
struct VariantWrapper
{
//the type for the variant
typedef boost::variant<T0,T1,T2> VariantType;
//The visitor class for this variant
struct Visitor : public boost::static_visitor<>
{
void operator()(T0 value)
{
Process(value);
}
void operator()(T1 value)
{
Process(value);
}
void operator()(T2 value)
{
Process(value);
}
virtual void Process(T0 val){/*do nothing */}
virtual void Process(T1 val){/*do nothing */}
virtual void Process(T2 val){/*do nothing */}
protected:
Visitor(){}
};
typedef Visitor VisitorType;
private:
VariantWrapper(){}
};
The class is then used as follows:
typedef VariantWapper<bool,int,double> VariantWrapperType;
typedef VariantWrapperType::VariantType VariantType;
typedef VariantWrapperType::VisitorType VisitorType;
struct Visitor : public VisitorType
{
void Process(bool val){/*do something*/}
void Process(int val){/*do something*/}
/* this class is not interested in the double value */
};
VariantType data(true);
apply_visitor(Visitor(),data);
As I say, this seems to work okay but I would prefer it if I didn't have to create a special wrapper class to tie the variant and the visitor together. I would prefer to be able just to use a boost.variant directly to instantiate the template visitor class. I've had a look at using type parameters, non-type parameters and template template parameters but nothing seems to suggest itself. Is what I am trying to do not possible? I may be missing something, and would appreciate it if anyone has any input on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
带有 Boost Variant 和虚拟调度的代码有点可疑。特别是考虑到您知道在编译时对处理什么感兴趣,并且绝对不需要在运行时创建虚拟表来实现您的目标。
我建议您使用部分模板专业化。因此,有一个默认的模板方法,它可以接受变体中的任何类型并且不执行任何操作。对于您感兴趣的类型,只需专门化模板即可。
这是一个例子。我们有三种类型 - Foo、Bar 和 War。我们只对最后两种类型感兴趣,并且对它们进行了专门研究。所以 Foo 被忽略了。
这是一个简单的用法示例:
该程序的输出将是:
这是另一个解决方案。我们创建一个默认访问者,忽略除您在类型列表中指定的内容之外的所有内容。这并不方便,因为您必须指定类型列表两次 - 一次在类型列表中,然后在每个处理方法(运算符)中。另外,事实上,通用模板将继承您的访问者。但尽管如此,我们还是要说:
The code with Boost Variant and virtual dispatching is a little fishy. Especially taking into account that you know what are you interested in processing during the compile-time and there is absolutely no need in creating a virtual table at run-time in order to achieve your goals.
I would recommend you use partial template specialization. Thus, have a default template method that can accept any type in the variant and will do nothing. For those types you are interested in, just specialize template.
Here is an example. We have three types - Foo, Bar and War. We are interested only in the last two types and have a specialization for them. So Foo is being ignored.
Here is a simple example of the usage:
The output of this program will be:
Here is another solution. We create a default visitor ignoring everything, except what you have specified in a type list. It is not that convenient because you have to specify a list of types twice - once in a type list and then in each processing method (operator). Plus, the generic template, in fact, will be inheriting your visitor. But nevertheless, here we go:
随着时间的推移,新的、有趣的图书馆不断涌现。这个问题很老了,但从那时起就有了一个解决方案,对我个人来说,它比迄今为止给出的解决方案要优越得多。
优秀的 Mach7 库允许前所未有的匹配(因此访问)功能。它由尤里·索洛德基 (Yuriy Solodkyy)、加布里埃尔·多斯·雷斯 (Gabriel Dos Reis) 和比亚尼·斯特鲁斯特鲁普 (Bjarne Stroustrup) 本人撰写。对于那些在这个问题上遇到困难的人,这里有一个来自自述文件的例子:
我现在正在使用它,到目前为止,使用它真的很愉快。
As time passes new and interesting libraries develop. This question is old, but since then there is a solution that to me personally is far more superior to the ones that have been given so far.
The excellent Mach7 library which allows unprecedented matching (and therefore visiting) capabilities. It is written by Yuriy Solodkyy, Gabriel Dos Reis and Bjarne Stroustrup himself. For the ones stumbling on this question, here is an example taken from the README:
I am working with it now and so far it is a real pleasure to use.
汤姆,我相信你的问题在特定的背景下很有意义。假设您想要将多种类型的访问者存储在向量中,但您不能这样做,因为它们都是不同的类型。你有几个选择:再次使用variant来存储访问者,使用boost.any,或者使用虚函数。我认为虚函数是一种优雅的解决方案,但肯定不是唯一的解决方案。
事情是这样的。
首先,让我们使用一些变体;
bool
、int
和float
就可以了。然后是基类,或多或少与您所拥有的一样。
接下来,我们有两个特定的变体。
最后,我们可以制作不同变体的单个向量:
这是输出:
如果由于某种原因我需要在一个容器中拥有不同的变体,我肯定会考虑这种解决方案。我还会想,如果将访问者真正困在另一个变体中,会变得更糟/更好。使用继承的好处在于它是可扩展的:您始终可以从类继承,但是一旦设置了变体,您就无法在不实际接触现有代码的情况下更改它。
Tom, I believe that your question makes much sense in a particular context. Say that you want to store visitors of multiple types in a vector, but you can't because they are all of different types. You have a few choices: use variant again to store visitors, use boost.any, or use virtual functions. I think that virtual functions are an elegant solution here, but certainly not the only one.
Here is how it goes.
First, let's use some variant;
bool
,int
, andfloat
will do.Then comes the base class, more or less as you had it.
Next, we have two specific variants.
Finally, we can make a single vector of different variants:
And here is the output:
If for some reason I needed to have different variants in one container, I would surely consider this solution. I would also think how much worse/better would it be to actually stick the visitors into another variant. The nice thing about using inheritance is that it is extensible post factum: you can always inherit from a class, but once a variant is set, you can't change it without actually touching the existing code.