I came across an older pondering Type erasure techniques, by Xeo, and I became to wonder, how should one to amend that code to make it work with std::unique_ptr'ers
and std::shared_ptr'ers
.
The code in the post can be found here. The code won't compile if fed with something containing unique_ptrs
and the data in shared_ptr'ers
become garbage. What I tried was a class inherited from a templated base class, so maybe it was somewhat complicated too. Now, this is mainly out of curiosity, as I became to wonder if it would be difficult (in general case) as this could become handy when storing complex objects, say, in std::vector
when Boost.Any isn't available for use.
Edit: I noticed I just had a bug in my code whilst testing, the code works just fine with shared_ptr'ers
(the contents aren't garbage), though not with unique_ptr'ers
. And then also, why not store a newed instance of this type erasured Any_Virtual
(as in code provided by Xeo) to, say, std::unique_ptr'ers
.
I guess then the questions would be:
- How to amend the
Any_Virtual
so that it could work withstd::unique_ptr
? - Which one would be better design, a
std::vector<Any_Virtual>
objects, whereAny_Virtual
holds a smart pointer, or astd::vector<std::unique_ptr<Any_Virtual>>
objects? Or does it even matter?