boost::function 和多参数成员函数

发布于 2024-10-07 00:32:43 字数 1090 浏览 0 评论 0原文

我对 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不接受 2 arg 成员函数,在文档中它说我应该使用 boost::bind,但我不确定我会如何做到这一点,因为我也必须满足我的 boost::apply_visitor 函数。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぇ气 2024-10-14 00:32:43

如果没有看到 ConcatValue 的声明,很难精确,但你想要类似的东西:

boost::bind(&Decomposer::ConcatValues, some_decomposer_instance, _1, _2)

It's hard to be precise without seeing ConcatValue's declaration, but you want something like:

boost::bind(&Decomposer::ConcatValues, some_decomposer_instance, _1, _2)
忆梦 2024-10-14 00:32:43

您需要提供 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);

再可℃爱ぅ一点好了 2024-10-14 00:32:43

答案取决于 ConcatValues 到底是什么。我猜测它实际上是 Decomposer 类的非静态成员函数,因此它实际上不需要两个参数,而是三个参数:两个字符串以及调用它的 Decomposer 实例 (this)。

Boost.Bind 确实是一个解决方案,因为它将帮助您将成员函数的第一个参数绑定到 Decomposer 实例,并转发两个 std::string 通过:

Decomposer decomposer;
boost::apply_visitor(
   add_node_value_visitor(boost::bind(&Decomposer::ConcatValues, &decomposer, _1, _2), var
);

The answer depends on what ConcatValues really is. I'm guessing that it is actually a non-static member function of a Decomposer class, and that as such it doesn't actually expect two but three parameters : two strings and the Decomposer 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 two std::string passed :

Decomposer decomposer;
boost::apply_visitor(
   add_node_value_visitor(boost::bind(&Decomposer::ConcatValues, &decomposer, _1, _2), var
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文