lundi 31 juillet 2017

std::forward and factory that calls std::make_shared

I have code like this...

X* raw = ::new X("abc");
// do something benign to object
std::shared_ptr<X> ptr(raw);

Now consider the same using an object factory...

template<typename T, typename... A>
std::shared_pointer<T> factory(A&&... args) {
    auto ptr = std::make_shared<T>(std::forward<A>(args)...);
    // do something benign to object
    return ptr;
}

std::shared_ptr<X> ptr = factory<X>("abc");    

This is a simplified example, but I'm seeing an unexplained crash when using the factory that appears to be due to a corrupted shared pointer.

I'm not familiar with the innards of std::make_shared, but am wondering if it in turn does something like argument forwarding to placement ::new, and that this chain forwarding is a problem.

Aucun commentaire:

Enregistrer un commentaire