Is shared_ptr supported in class member derived from TObject?

ROOT does not support saving any class containing smart pointers like std::shared_ptr or std::unique_ptr

ROOT fully support std::unique_ptr.

We have not found a good semantic to support shared_ptr. We could ‘easily’ store the pointee of a shared_ptr but we have no answer to the question of what to do if several shared_ptr to the same pointee are stored accross I/O transaction: for example in separate split branch or separate top level branch or even in separate TKey (in object stored directly in the TFile). Do we store a copy for each of those cases? (leading to duplication of the data on disk). When reading back do we keep the related shared_ptr independent? Implementing the ‘yes’ version to those 2 questions is straight-forward, but is it the right semantic? Is it what the user expected? Most likely it is not, in particular the reading back part (upon reading the shared_ptr would not be sharing anything …). Implementing the no version to either of those question is challenging. The existing version of that is the TRef and TRefArray classes which requires instrumentation of the pointee class (It must derived from TObject) and have found both challenging to use and challenging to maintain.

So until we can resolved the semantic (and technical) issue surrounding std::shared_ptr, we are leaving it unsupported (as well std::weak_ptr etc.)

Or when trying to save std::unique_ptr:

Error in <TStreamerInfo::Build>: myDetectorData, unknown type: int int_ptr

This is a different issue. In this case, I see (at the time I retrieve your repository):

class myDetectorData {
....
    std::shared_ptr<int> int_ptr;
};

so I guess you might have change it to:

class myDetectorData {
....
    std::unique_ptr<int> int_ptr;
};

which is not support ‘as-is’ since we guess that have a pointer to a single int is … inefficient, we assume that the pointer to int is actually a pointer to an array of ints and you would need to provide the size (the number of elements in the array) (there is a syntax for it but it is not recommended). And at that pointer you are better off using a `std::vector. So for that case use either:

class myDetectorData {
....
    int int_value;
};

or

class myDetectorData {
....
    std::vector<int> int_values;
};
2 Likes