0

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?

hllnll
  • 374
  • 1
  • 2
  • 8

1 Answers1

4

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480