generic flyweighting function I made this little function for flyweighting any type that has operator&lt;. I followed the convention of make_unique and make_shared with make_one. Not sure if that's the best name.

// Create a single instance of an object, using
// operator&lt; to determine equality.
// Note that we take a value instead of a constructor
// arguments because often we'll want to mutate a field,
// then create a new object, without passing in everything.
template&lt;class T&gt;
std::shared_ptr&lt;const T&gt; make_one(const T&amp; value) {

  struct cmp {
    bool operator()(const T* a, const T* b) const {
      return *a &lt; *b;
    }
  };

  static std::set&lt;const T*, cmp &gt; values;

  auto iter = values.find(&amp;value);

  if(iter != values.end()) {
    return (*iter)-&gt;shared_from_this();
  }

  auto ptr = std::shared_ptr&lt;T&gt;(new T(value), [](T* p) {
    values.erase(p); delete p;
  });
  values.insert(ptr.get());
  return ptr;

}


Test code:

  struct Foo : std::enable_shared_from_this&lt;Foo&gt; {
    int x = 0;
    Foo(int x) : x(x) { }
    bool operator&lt;(Foo rhs) const { return x &lt; rhs.x; }
  };

  auto p0 = make_one(Foo{0});
  auto p1 = make_one(Foo{0});
  assert(p0 == p1);

  auto p2 = make_one(Foo{42});
  assert(p2 != p0);


Review goals:


Can I avoid requiring enable_shared_from_this?
Can I make it thread safe without too much trouble?
Any other ideas for improvement.
The first thing I would note is that you should point out non obvious features of your implementation. The nice thing that is not immediately obvious is that when all external references are gone the object is automatically deleted from the make_one()::values set.

This is not really a requirement of a flyweight.
But a nice little trick here.

So if the automatic deletion is required then the answers to the question are:


  Can I avoid requiring enable_shared_from_this?


No. I don't think so.  

You could re-invent some reference counting model but that seems a lot of work in comparison to simply using the standard libraries.

As an alternative, You can get around it slightly by using std::set&lt;std::weak_ptr&lt;T&gt;&gt; the trouble here is that even though the resource would be cleaned up the actual space for the resource would be maintained after the object is destroyed.

If on the other hand automatic deletion is not required.


  Can I avoid requiring enable_shared_from_this?


Yes. there are simpler ways that this could be written if there is no requirement to delete upon all external references going away. Now if you want it again it will still be there.

template&lt;class T&gt;
T const&amp; make_one(const T&amp; value) {

    static std::set&lt;T&gt; values;

    auto result = values.insert(value);
    return *(result.first);
}



  Can I make it thread safe without too much trouble?


Yes simply add a lock on the make_one()


  Any other ideas for improvement.


You could add the ability to move an object into your make_once() rather than copy it each time. Even better would be to allow the construction of T from its parameters.

// Move an object in your set
template&lt;typename T&gt;
std::shared_ptr&lt;const T&gt; make_one(T&amp;&amp; value)

// Emplace a value in your set
template&lt;typename T, typename... Args&gt;
std::shared_ptr&lt;const T&gt; make_one(Args&amp;&amp;... value)