Boost.Flyweight模块的holder测试
Boost.Flyweight是一个C++库,用于实现轻量级对象的共享,具有高性能和低内存使用率。其中,Flyweight模式就是通过共享相同的对象,来减少应用程序的内存占用。而holder是其中的一种策略,它通过引入一个内部持有者来保持被共享对象的状态,从而避免了在共享时将其状态暴露出去。
为了测试Boost.Flyweight的holder策略,我们可以编写一个简单的示例程序。以下是一个基于holder策略的例子:
#include <iostream>
#include <boost/flyweight.hpp>
using namespace boost::flyweights;
struct Person{
std::string name;
Person(std::string n):name(n){}
};
struct NameGetter{
std::string operator()(const Person& p) const{
return p.name;
}
};
typedef flyweight<Person, holder<ValueType<Person>>, NameGetter> SharedPerson;
int main(){
SharedPerson alice = Pers