I have the following piece of code
// DoSomething.h
class Foo;
void doSomething(std::optional<std::unique_ptr<const Foo>> f = std::nullopt);
If I include DoSomething.h
anywhere without the definition of Foo
the file doesn't compile, even if it doesn't even call the doSomething
function. In contrast, removing the =std::nullopt
default argument, everything compiles fine.
My assumption is the issue has something to do with the deleter interface of the unique_ptr, and my question is how to solve this issue so I can forward declare Foo
in this scenario?
Thanks
Edit: I don't want to change the signature of this function, I know I can solve this with overloading or removing the optional use. I want to understand why this doesn't work.
Following note - this same code compiles perfectly fine when switching the std::unique_ptr
with std::shared_ptr
. It means it should be possible to make it work with std::unique_ptr
. I know their deleteres have slightly different interface, and I'm not familiar enough with it.