传入一个函数来获取一个或多个返回值
我有一个如下所示的模板:
struct add_node_value_visitor : boost::static_visitor<>
{
add_node_value_visitor(){}
template <typename T>
void operator() ( const T& value) const
{
boost::lexical_cast<std::string, T>(value);
}
};
我遇到的问题是访问者在 for 循环内使用,迭代一堆值,并且生成的值字符串需要是一个字符串,目前这会产生一堆单独的字符串,这不是我想要的,所以为了解决这个问题,我想我应该添加一个指向该结构体的函数指针,以便我可以传入一个函数来连接每个循环迭代的结果字符串并创建一个字符串。然后,如果我想在不需要连接的地方使用这个结构,我仍然可以这样做。问题是我是否应该使用函数指针,或者是否可以使用 boost::lambda 之类的东西来做到这一点?
或者 boost::function 会更容易使用吗?
I have a template looking like this:
struct add_node_value_visitor : boost::static_visitor<>
{
add_node_value_visitor(){}
template <typename T>
void operator() ( const T& value) const
{
boost::lexical_cast<std::string, T>(value);
}
};
The problem I have is that the visitor is used inside a for loop iterating over a bunch of values, and the resulting string of values needs to be one string, currently this would produce a bunch of separate strings, which is not what I want, so to solve this problem I thought I'd add a function pointer to the ctor of this struct so that I can pass in a function to concatenate the resulting string of each loop iteration and create one string. Then if I want to use this struct where I do not need a concatenation, I can still do that. The question is whether I should use a function pointer or is it possible to do this with something like boost::lambda?
Or would boost::function be easier to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西吗?
Something like this?
为什么不就地执行串联?
Why not performing the concatenation in place?
也许更通用的访问者会更合适,将如何处理字符串表示留给调用者。
Perhaps a more general-purpose visitor would be in order, leaving it up to the caller what to do with the string representations.