2

I have a class foo that derives from base, and hence have a virtual destructor. Class foo implements the pimpl idiom with a std::unique_ptr of the incomplete type foo::impl. Like you can see in the code below, it doesn't compile as expected if I try to delete a foo instance because std::unique_ptr needs to know foo::impl. On the other hand, it does compile when I delete it through its base class. I don't understand why it does so and more importantly I would like to know if it is undefined behavior.

// Header.h
#pragma once

#include <memory>

class Base
{
public:
  virtual ~Base() =default;
};

class foo: public Base
{
public:
  foo();

private:
  class impl;
  std::unique_ptr<impl> p_;
};
// Source.cpp
#include "Header.h"

class foo::impl
{
};

foo::foo():p_(std::make_unique<foo::impl>()) {};
// main.cpp
#include "Header.h"

int main()
{
  std::unique_ptr<Base> t = std::make_unique<foo>(); // this one compiles and links and I would have expected it not to.

  //foo b; // this one does not compile as expected
  return 1;
}

I have the same results with Visual Studio 2013 and Visual Studio 2017.

0 Answers0