Search code examples
c++shared-ptrcopy-constructor

Prevent copy-construction with make_shared


I have a manager class that allow clients to add components through two methods: one with no argument that default-construct the component and another one that takes an rvalue (which should allow client to use a custom constructor of the component).

Here's the code I came up with:

template <class TComponent>
std::shared_ptr<TComponent> AddComponent()
{
    return AddComponent(TComponent{ this });
}

template <class TComponent>
std::shared_ptr<TComponent> AddComponent(const TComponent &&obj)
{
    auto ptr = std::make_shared<TComponent>(obj);
    vec.push_back(ptr);
    return ptr;
}

The problem I have is that std::make_shared always copy-constructs the object. Is there a way to prevent this behavior? I read about perfect forwarding but it doesn't seem to help here.


Solution

  • I read about perfect forwarding but it doesn't seem to help here.

    I don't see why it wouldn't.

    Simply remove the const to make move construction possible, and add std::forward:

    template <class TComponent>
    std::shared_ptr<TComponent> AddComponent(TComponent &&obj)
    {
        auto ptr = std::make_shared<TComponent>(std::forward<TComponent>(obj));
        vec.push_back(ptr);
        return ptr;
    }
    

    Now, rvalues will be moved. Lvalues will be copied which you cannot avoid.