Atomic <ReferenceCountedObjectPtr <Object> >?

struct Object : ReferenceCountedObject
{
  typedef ReferenceCountedObjectPtr <Object> Ptr;
};

struct Container
{
  Atomic <Object::Ptr> m_object;

  void set (Object::Ptr object)
  {
    Object::Ptr prev = m_object.exchange (object);
    // use prev
  }
};

Is this guaranteed to work? or does the template argument to Atomic<> have to be a primitive type (i.e. have trivial constructor and destructors)?

It certainly compiles…

Or is the behavior of such a construct identical to Atomic <Object*> (i.e. no addref or releasing)

Does that even compile??

No, the atomic class can only use primitive types - it really just treats the value as a 32 or 64-bit blob of memory.

I guess you could just use an Atomic<Object*> and inc/dec its reference count manually if you really need the pointer exchange to be atomic.