0
votes

The following code compiles fine with gcc 4.8.1

#include <memory>

class Foo {
public:
     explicit Foo(const std::shared_ptr<Foo>& foo) {

     }
};

int main() {
    Foo foo(nullptr);
}

Why is this possible? Shouldn't the explicit prevent the compiler from calling std::shared_ptr(nullptr) implicitly?

1
As a note, a universal or otherwise template constructor can use SFINAE to do what you want. - Yakk - Adam Nevraumont

1 Answers

4
votes

Shouldn't the explicit prevent the compiler from calling std::shared_ptr(nullptr) implicitly?

No, the explicit constructor would stop this from happening:

Foo foo = some_shared_ptr;

It has no effect on the constructors of shared_ptr, so implicit conversion from nullptr to shared_ptr is still allowed.