boost::function 和多参数成员函数
我对 boost::function 对象有以下定义:
typedef boost::function<std::string (std::string, std::string)> concat;
我将此函数作为结构构造函数参数传递:
struct add_node_value_visitor : boost::static_visitor<>
{
typedef boost::function<std::string (std::string, std::string)> concat;
add_node_value_visitor(concat _func, std::string key) : _func_concat(_func), _key(key) {}
template <typename T>
void operator() ( const T& value) const
{
std::string result = _func_concat(boost::lexical_cast<std::string, T>(value), _key);
}
std::string _key;
concat _func_concat;
};
现在我需要将 struct add_node_value_visitor 传递给以下函数,但是 boost ::function
boost::apply_visitor( add_node_value_visitor( &Decomposer::ConcatValues, key), var); // ConcatValues takes 2 args, var = boost::variant
std::string ConcatValues(std::string str, std::string key);
有人有什么想法吗?
I have the following definition of a boost::function object:
typedef boost::function<std::string (std::string, std::string)> concat;
I am passing this function as a struct constructor argument:
struct add_node_value_visitor : boost::static_visitor<>
{
typedef boost::function<std::string (std::string, std::string)> concat;
add_node_value_visitor(concat _func, std::string key) : _func_concat(_func), _key(key) {}
template <typename T>
void operator() ( const T& value) const
{
std::string result = _func_concat(boost::lexical_cast<std::string, T>(value), _key);
}
std::string _key;
concat _func_concat;
};
Now I need to pass the struct add_node_value_visitor
to the following function, however the boost::function<T>
does not accept the 2 arg member function, in the documentation it says I should use boost::bind, but I am however not sure how I'd do that, seeing I also have to satisfy my boost::apply_visitor function.
boost::apply_visitor( add_node_value_visitor( &Decomposer::ConcatValues, key), var); // ConcatValues takes 2 args, var = boost::variant
std::string ConcatValues(std::string str, std::string key);
Any ideas anybody?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有看到 ConcatValue 的声明,很难精确,但你想要类似的东西:
It's hard to be precise without seeing ConcatValue's declaration, but you want something like:
您需要提供 Decomposer 对象的实例,如下所示:
boost::apply_visitor( add_node_value_visitor( boost::bind( &Decomposer::ConcatValues, yourDecomposer, _1, _2 ), key), var);< /代码>
You need to supply an instance of a Decomposer object, like this:
boost::apply_visitor( add_node_value_visitor( boost::bind( &Decomposer::ConcatValues, yourDecomposer, _1, _2 ), key), var);
答案取决于
ConcatValues
到底是什么。我猜测它实际上是 Decomposer 类的非静态成员函数,因此它实际上不需要两个参数,而是三个参数:两个字符串以及调用它的Decomposer
实例 (this
)。Boost.Bind 确实是一个解决方案,因为它将帮助您将成员函数的第一个参数绑定到 Decomposer 实例,并转发两个 std::string 通过:
The answer depends on what
ConcatValues
really is. I'm guessing that it is actually a non-static member function of aDecomposer
class, and that as such it doesn't actually expect two but three parameters : two strings and theDecomposer
instance on which it is invoked (this
).Boost.Bind is indeed a solution as it will help you bind the first argument of the member function to a
Decomposer
instance, and forward the twostd::string
passed :