Boost.flyweight 和 Boost.MPL

发布于 2024-12-10 19:13:15 字数 1267 浏览 1 评论 0原文

我有一个关于享元选项的问题,给出下面的定义,基于 http://www.boost.org/doc/libs/1_40_0/libs/flyweight/test/test_basic.cpp

typedef boost::flyweights::flyweight<
    std::string, 
    boost::flyweights::tag<int>,
    boost::flyweights::static_holder_class<boost::mpl::_1>,          
    boost::flyweights::hashed_factory_class<
        boost::mpl::_1, 
        boost::mpl::_2, 
        boost::hash<boost::mpl::_2>,
        std::equal_to<boost::mpl::_2>,
        std::allocator<boost::mpl::_1>
    >,
    boost::flyweights::simple_locking,
    boost::flyweights::refcounted
> StringFlyweight;

StringFlyweight    test1("Hello World");

有什么价值boost::mpl::_1boost::mpl::_2 ?什么时候分配?

boost::mpl::_1 很可能是 std::stringboost::mpl::_2 应该是 size_t 吗?如果属实,如何扣除? 我不明白 key_type 是如何选择的。

我已阅读http://www.boost。 org/doc/libs/1_41_0/libs/flyweight/doc/tutorial/lambda_expressions.html 但我这是我第一次接触 Boost.MPL 并且还不够:)

I have a question regarding flyweight options, given the definition below, based on http://www.boost.org/doc/libs/1_40_0/libs/flyweight/test/test_basic.cpp

typedef boost::flyweights::flyweight<
    std::string, 
    boost::flyweights::tag<int>,
    boost::flyweights::static_holder_class<boost::mpl::_1>,          
    boost::flyweights::hashed_factory_class<
        boost::mpl::_1, 
        boost::mpl::_2, 
        boost::hash<boost::mpl::_2>,
        std::equal_to<boost::mpl::_2>,
        std::allocator<boost::mpl::_1>
    >,
    boost::flyweights::simple_locking,
    boost::flyweights::refcounted
> StringFlyweight;

StringFlyweight    test1("Hello World");

what value has boost::mpl::_1 and boost::mpl::_2 ? When are asigned ?

boost::mpl::_1 is most probably std::string. boost::mpl::_2 should be size_t ? If true, how is deducted ?
I don't understand how is key_type chosen.

I have read http://www.boost.org/doc/libs/1_41_0/libs/flyweight/doc/tutorial/lambda_expressions.html but I it is my first contact with Boost.MPL and is not enough :)

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

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

发布评论

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

评论(1

攒一口袋星星 2024-12-17 19:13:15

boost::mpl::_1boost::mpl::_2 是占位符;它们可以用作模板参数,以区分稍后与实际参数的绑定。这样,您可以进行部分应用(将具有 n 元数的元函数转换为具有 (nm) 元数的函数)、lambda 表达式(在需要时动态创建元函数)等

。至少包含一个占位符是一个占位符表达式,可以像任何其他元函数一样调用它,并使用一些将替换占位符的参数。

在您的示例中,假设使用以下 typedef,

typedef boost::flyweights::hashed_factory_class<
    boost::mpl::_1, 
    boost::mpl::_2, 
    boost::hash<boost::mpl::_2>,
    std::equal_to<boost::mpl::_2>,
    std::allocator<boost::mpl::_1>
> hashed_factory;

我们可以假设在代码中的某个其他点,将使用某个参数调用 hashed_factory

typedef typename
    boost::mpl::apply<
       hashed_factory,
       X,
       Y
    >::type result; // invoke hashed_factory with X and Y
                    // _1 is "replaced" by X, _2 by Y

我没有查看 Flyweight 代码,但我们可以假设 < code>_1 将绑定到享元的值类型,_2 将绑定到键类型(因为它用于散列和测试相等性)。在这种情况下,我认为两者都将是 std::string 因为没有指定密钥类型。

我不确定我对 MPL 占位符的解释是否清楚,请随意阅读 优秀的 MPL 教程,很好地解释了元函数、lambda 表达式和其他模板元编程功能。

boost::mpl::_1 and boost::mpl::_2 are placeholders; they can be used as template parameters to differ the binding to an actual argument to a later time. With this, you can do partial application (transforming a metafunction having an n-arity to a function having a (n-m)-arity), lambda expressions (creating a metafunction on-the-fly where it is needed), etc.

An expression containing at least a placeholder is a placeholder expression, which can be invoked like any other metafunction, with some arguments that will replace the placeholders.

In your example, assuming the following typedef

typedef boost::flyweights::hashed_factory_class<
    boost::mpl::_1, 
    boost::mpl::_2, 
    boost::hash<boost::mpl::_2>,
    std::equal_to<boost::mpl::_2>,
    std::allocator<boost::mpl::_1>
> hashed_factory;

we can assume that at some other point in the code, the hashed_factory will be invoked with some parameter:

typedef typename
    boost::mpl::apply<
       hashed_factory,
       X,
       Y
    >::type result; // invoke hashed_factory with X and Y
                    // _1 is "replaced" by X, _2 by Y

I did not look in Flyweight code, but we can suppose that _1 will be bound to the value type of the flyweight, and _2 to the key type (since it is used for hashing and testing equality). In this case, I think both will be std::string since no key type is specified.

I'm not sure my explanation about MPL's placeholders is quite clear, feel free to read the excellent MPL tutorial that explains very well metafunctions, lambda expressions and other template metaprogramming features.

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