<r <at> primef.actor> writes:
Dear all,
On a project of mine, I started to use Boost.Flyweight. I would like users of my class to only be able to instantiate objects of my class by using the Boost.Flyweight mechanism. For this, I make my class constructor private. However, when doing so the corresponding flyweight factory also cannot instantiate my class. What classes do I need to `friend` in order to make this work?
You need to do template<typename,typename,typename> friend struct boost::flyweights::detail::optimized_key_value; but this is highly unadvisable, as you're relying on undocumented classes that might change without prior notice in future versions of the library. An alternative is to simply make the whole class implementation private: class X { struct impl { impl(int x):m_data(x){}; int data() const { return m_data; } int m_data; }; struct key_extractor { int operator()(const impl& x) const { return x.data(); } }; public: typedef boost::flyweights::flyweight< boost::flyweights::key_value< int, impl, key_extractor > > flyweight; }; int main() { auto my_x = X::flyweight(42); return 0; } Joaquín M López Muñoz Telefónica