g++ -std=c++11
does not compile a class that contains a container that contains unique pointers that point to a forward declared class. Questions:
- Why?
- Is there a reasonable workaround?
Code example:
#include <vector>
#include <memory>
// variant 1 (with full class definition): compiles
class Bar { [..] };
using BarPtr = std::unique_ptr<Bar>;
// variant 2 (with shared instead of unique pointers): compiles
using BarPtr = std::shared_ptr<class Bar>;
// variant 0 (which is what we want): compilation fails below
using BarPtr = std::unique_ptr<class Bar>;
// end of variants
class Foo {
std::vector<BarPtr> vec;
public:
Foo() {} // compilation of variant 0 fails here:
// In instantiation of ‘void std::default_delete<Bar>::operator()(Bar*) const
// ...
// invalid application of ‘sizeof’ to incomplete type ‘Bar’
};
I have seen How to forward declare a class to be used in a standard container of unique_ptr and Is std::unique_ptr<T> required to know the full definition of T?, but do not find convincing answers to my above questions.