vendredi 11 août 2017

Why is the convert constructor for unique_ptr not explicit?

What is the reason behind not making the convert constructor for std::unique_ptr explicit? A possible bug that one might see is for example the following

void foo(std::unique_ptr<Base>&& obj) { ... }

int main() {
    auto derived_ptr = std::make_unique<Derived>();
    foo(derived_ptr);
    derived_ptr->access();
}

Here although the writer of foo intended to only accept rvalues to unique_ptr of type Base, they might accidentally end up taking ownership of a pointer of another type.

Aucun commentaire:

Enregistrer un commentaire