Boost.flyweight 和 Boost.MPL
我有一个关于享元选项的问题,给出下面的定义,基于 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::_1
和 boost::mpl::_2
?什么时候分配?
boost::mpl::_1
很可能是 std::string
。 boost::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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
boost::mpl::_1
和boost::mpl::_2
是占位符;它们可以用作模板参数,以区分稍后与实际参数的绑定。这样,您可以进行部分应用(将具有 n 元数的元函数转换为具有 (nm) 元数的函数)、lambda 表达式(在需要时动态创建元函数)等。至少包含一个占位符是一个占位符表达式,可以像任何其他元函数一样调用它,并使用一些将替换占位符的参数。
在您的示例中,假设使用以下 typedef,
我们可以假设在代码中的某个其他点,将使用某个参数调用
hashed_factory
:我没有查看 Flyweight 代码,但我们可以假设 < code>_1 将绑定到享元的值类型,
_2
将绑定到键类型(因为它用于散列和测试相等性)。在这种情况下,我认为两者都将是std::string
因为没有指定密钥类型。我不确定我对 MPL 占位符的解释是否清楚,请随意阅读 优秀的 MPL 教程,很好地解释了元函数、lambda 表达式和其他模板元编程功能。
boost::mpl::_1
andboost::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
we can assume that at some other point in the code, the
hashed_factory
will be invoked with some parameter: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 bestd::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.