C++ Boost.flyweight和Boost.MPL
我有一个关于flyweight选项的问题,根据以下定义,基于C++ Boost.flyweight和Boost.MPL,c++,templates,boost,boost-mpl,C++,Templates,Boost,Boost Mpl,我有一个关于flyweight选项的问题,根据以下定义,基于 typedef boost::flyweights::flyweight< std::字符串, boost::flyweights::标记, 助推::飞锤::静态\u支架\u类, boost::flyweights::散列工厂类< boost::mpl::_1, boost::mpl::_2, boost::hash, 标准::等于, 分配器 >, boost::flyweights::简单锁定, boost::flyweights:
typedef boost::flyweights::flyweight<
std::字符串,
boost::flyweights::标记,
助推::飞锤::静态\u支架\u类,
boost::flyweights::散列工厂类<
boost::mpl::_1,
boost::mpl::_2,
boost::hash,
标准::等于,
分配器
>,
boost::flyweights::简单锁定,
boost::flyweights::refcounted
>飞锤;
StringFlyweight测试1(“Hello World”);
什么值具有boost::mpl::_1
和boost::mpl::_2
?什么时候开始
boost::mpl::_1
很可能是std::string
<代码>增强::mpl::_2应该是大小\u t
?如果为真,如何扣除?
我不明白钥匙是怎么选择的
我读过,但我认为这是我第一次接触Boost.MPL,这还不够:)
Boost::MPL::_1
和Boost::MPL::_2
是占位符;它们可以用作模板参数,以便将与实际参数的绑定区别开来。有了它,您可以进行部分应用(将具有n-arity的元函数转换为具有(n-m)-arity的函数)、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;
我没有查看Flyweight代码,但我们可以假设\u 1
将绑定到Flyweight的值类型,\u 2
绑定到键类型(因为它用于散列和测试相等性)。在本例中,我认为两者都是std::string
,因为没有指定键类型
我不确定我对MPL占位符的解释是否很清楚,请随意阅读解释了非常好的元函数、lambda表达式和其他模板元编程功能的说明。因此,键类型由flyweight决定。但是我如何确定类型并最终设置一个呢?键是整数类型而不是字符串非常重要。如果要指定用于检索对象的键,需要使用
key\u value
作为flyweight
的参数。您还需要告诉库如何使用“键提取器”(一个返回与作为参数给定的值对应的键的函子)从值中检索键。有关示例,请参见。
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;
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