Adding an object containing a ScopedPointer to a collection


#1

Here's a contrived example:

// Does not compile - ListItem has no copy constructor available
struct ListItem
{
    ListItem(File* initialFile) : file(initialFile) { }

    ScopedPointer<File> file;
};

Array<ListItem> List;

List.add(ListItem(nullptr));

This won't compile because List.add() needs to take a copy of the temporary ListItem in order to do its in-place new.  Now, scoped pointers do allow copies (by transferring ownership of their pointer from the source to the dest object), but since the temporary copy is passed to List.add() as a const reference, it can't modify the source object to transfer the ownership.

Is this a good place to use OwnedArray in place of Array, and pass in a heap allocated ListItem?


#2

Why not just use std::vector and std::unique_ptr?