如何使用 Boost.Variant 迭代一系列有界类型
struct A
{
std::string get_string();
};
struct B
{
int value;
};
typedef boost::variant<A,B> var_types;
std::vector<var_types> v;
A a;
B b;
v.push_back(a);
v.push_back(b);
如何迭代 v 的元素来访问 a 和 b 对象?
我可以使用 boost::get 来做到这一点,但语法确实很麻烦。:
std::vector<var_types>:: it = v.begin();
while(it != v.end())
{
if(A* temp_object = boost::get<A>(&(*it)))
std::cout << temp_object->get_string();
++it;
}
我尝试使用访问技术,但我没有走得太远,代码无法正常工作:
template<typename Type>
class get_object
: public boost::static_visitor<Type>
{
public:
Type operator()(Type & i) const
{
return i;
}
};
...
while(it != v.end())
{
A temp_object = boost::apply_visitor(get_object<A>(),*it);
++it;
}
编辑1< /strong>
一个黑客解决方案是:
class get_object
: public boost::static_visitor<A>
{
public:
A operator()(const A & i) const
{
return i;
}
A operator()(const B& i) const
{
return A();
}
};
struct A
{
std::string get_string();
};
struct B
{
int value;
};
typedef boost::variant<A,B> var_types;
std::vector<var_types> v;
A a;
B b;
v.push_back(a);
v.push_back(b);
How can I iterate iterate through the elements of v to get access to the a and b objects ?
I'm able to do it with boost::get but the syntax is really cumbersome.:
std::vector<var_types>:: it = v.begin();
while(it != v.end())
{
if(A* temp_object = boost::get<A>(&(*it)))
std::cout << temp_object->get_string();
++it;
}
I've tried to use the visitation technique but I didn't get too far and the code is not working :
template<typename Type>
class get_object
: public boost::static_visitor<Type>
{
public:
Type operator()(Type & i) const
{
return i;
}
};
...
while(it != v.end())
{
A temp_object = boost::apply_visitor(get_object<A>(),*it);
++it;
}
EDIT 1
A hackish solution is :
class get_object
: public boost::static_visitor<A>
{
public:
A operator()(const A & i) const
{
return i;
}
A operator()(const B& i) const
{
return A();
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,访客应该完成这项工作。如果您只是想获取存储的值,那么
boost::get
- 如果我没记错的话 - 是预制的访问者。示例:
用法:
As far as I can see, the visitor is supposed to do the work. If you just want to get the stored value, then
boost::get
- if I'm not mistaken - is the pre-made visitor for that.Example:
Usage:
编辑:
如果正如UncleBens建议的那样,那么你可以简单地这样做:
原始:
static_vistor
的模板参数是其方法的返回类型。这意味着这两个类需要为单个访问者共享公共返回类型。它应该看起来像这样:这是使用此访问者进行迭代的示例。
Edit:
If it's as UncleBens suggests, then you can simply do this:
Original:
The template parameter for
static_vistor
is the return type of its methods. This means the two classes need to share a common return type for a single visitor. It should look something like this:Here's an example of iteration using this visitor.