一般来说,Boost Bind 在幕后是如何工作的?

发布于 2024-07-06 21:37:35 字数 65 浏览 6 评论 0原文

在不花很长时间查看 boost 源代码的情况下,有人可以给我一个关于 boost Bind 是如何实现的快速概述吗?

Without spending a long time reviewing the boost source code, could someone give me a quick rundown of how boost bind is implemented?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

白云悠悠 2024-07-13 21:37:35

我喜欢这个 bind 源代码:

template<class R, class F, class L> class bind_t
{
public:

    typedef bind_t this_type;

    bind_t(F f, L const & l): f_(f), l_(l) {}

#define BOOST_BIND_RETURN return
#include <boost/bind/bind_template.hpp>
#undef BOOST_BIND_RETURN

};

告诉你几乎所有你需要知道的事情,真的。

bind_template 标头扩展为内联 operator() 定义的列表。 例如,最简单的:

result_type operator()()
{
    list0 a;
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

我们可以看到 BOOST_BIND_RETURN 宏此时扩展为 return,因此该行更像 return l_(type...)< /代码>。

单参数版本在这里:

template<class A1> result_type operator()(A1 & a1)
{
    list1<A1 &> a(a1);
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

非常相似。

listN 类是参数列表的包装器。 这里有很多深奥的魔法,但我并不太了解。 他们还重载了 operator() 来调用神秘的 unwrap 函数。 忽略一些编译器特定的重载,它不会做很多事情:

// unwrap

template<class F> inline F & unwrap(F * f, long)
{
    return *f;
}

template<class F> inline F & unwrap(reference_wrapper<F> * f, int)
{
    return f->get();
}

template<class F> inline F & unwrap(reference_wrapper<F> const * f, int)
{
    return f->get();
}

命名约定似乎是:Fbind 的函数参数的类型。 R 是返回类型。 L 往往是参数类型的列表。 由于不同数量的参数的重载不少于九个,因此也存在很多复杂性。 最好不要过多关注这一点。

I like this piece of the bind source:

template<class R, class F, class L> class bind_t
{
public:

    typedef bind_t this_type;

    bind_t(F f, L const & l): f_(f), l_(l) {}

#define BOOST_BIND_RETURN return
#include <boost/bind/bind_template.hpp>
#undef BOOST_BIND_RETURN

};

Tells you almost all you need to know, really.

The bind_template header expands to a list of inline operator() definitions. For example, the simplest:

result_type operator()()
{
    list0 a;
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

We can see the BOOST_BIND_RETURN macro expands to return at this point so the line is more like return l_(type...).

The one parameter version is here:

template<class A1> result_type operator()(A1 & a1)
{
    list1<A1 &> a(a1);
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

It's pretty similar.

The listN classes are wrappers for the parameter lists. There is a lot of deep magic going on here that I don't really understand too much though. They have also overloaded operator() that calls the mysterious unwrap function. Ignoring some compiler specific overloads, it doesn't do a lot:

// unwrap

template<class F> inline F & unwrap(F * f, long)
{
    return *f;
}

template<class F> inline F & unwrap(reference_wrapper<F> * f, int)
{
    return f->get();
}

template<class F> inline F & unwrap(reference_wrapper<F> const * f, int)
{
    return f->get();
}

The naming convention seems to be: F is the type of the function parameter to bind. R is the return type. L tends to be a list of parameter types. There are also a lot of complications because there are no less than nine overloads for different numbers of parameters. Best not to dwell on that too much.

暗地喜欢 2024-07-13 21:37:35

顺便说一句,如果通过包含 boost/bind/bind_template.hpp 来折叠和简化 bind_t ,它会变得更容易理解,如下所示:

template<class R, class F, class L> 
class bind_t
{
    public:

        typedef bind_t this_type;

        bind_t(F f, L const & l): f_(f), l_(l) {}

        typedef typename result_traits<R, F>::type result_type;
        ...
        template<class A1> 
            result_type operator()(A1 & a1)
            {
                list1<A1 &> a(a1);
                return l_(type<result_type>(), f_, a, 0);
            }
    private:
        F f_;
        L l_;

};

By the way, if bind_t is collapsed and simplified by including boost/bind/bind_template.hpp , it becomes easier to understand like the following :

template<class R, class F, class L> 
class bind_t
{
    public:

        typedef bind_t this_type;

        bind_t(F f, L const & l): f_(f), l_(l) {}

        typedef typename result_traits<R, F>::type result_type;
        ...
        template<class A1> 
            result_type operator()(A1 & a1)
            {
                list1<A1 &> a(a1);
                return l_(type<result_type>(), f_, a, 0);
            }
    private:
        F f_;
        L l_;

};
ゝ偶尔ゞ 2024-07-13 21:37:35

我认为它是一个模板类,它为要绑定的参数声明了一个成员变量,并为其余参数声明了重载 () 。

I think it's a template class that declares a member variable for the arguments you want to bind and overloads () for the rest of the arguments.

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