问题
Given that I have a forward declared type:
class Foo;
I want to make a unique_ptr to this type:
unique_ptr<Foo> pFoo;
This works fine in visual-studio-2017 but I can't make it work in visual-studio-2012.
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory(1150): error C2027: use of undefined type
Foo
(....\src\STETestbed\STETestbed.cpp)
O:\Engine\stetestbed\include\STETestbed\ComponentDirector.h(26) : see declaration ofFoo
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory(1149) : while compiling class template member functionvoid std::default_delete<_Ty>::operator ()(_Ty *) throw() const
with
[
_Ty=Foo
]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory(1444) : see reference to function template instantiationvoid std::default_delete<_Ty>::operator ()(_Ty *) throw() const
being compiled
with
[
_Ty=Foo
]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\type_traits(743) : see reference to class template instantiationstd::default_delete<_Ty>
being compiled
with
[
_Ty=Foo
]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory(1281) : see reference to class template instantiationstd::is_empty<_Ty>
being compiled
with
[
_Ty=std::default_delete
]
O:\Engine\stetestbed\include\STETestbed\ComponentDirector.h(63) : see reference to class template instantiationstd::unique_ptr<_Ty>
being compiled
with
[
_Ty=Foo
]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory(1151): error C2338: can't delete an incomplete type (....\src\STETestbed\STETestbed.cpp)
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory(1152): warning C4150: deletion of pointer to incomplete typeFoo
; no destructor called (....\src\STETestbed\STETestbed.cpp)
O:\Engine\stetestbed\include\STETestbed\ComponentDirector.h(26) : see declaration ofFoo
Was there a workaround for that back in the dark ages, or can I just not forward declare?
回答1:
As mentioned by Jarod42 The problem seems to be that the default_deleter
implemented by visual-studio-2012 required a complete type on declaration. Latter versions of visual-studio only required the complete type at the point the parenthesis operator is called.
We can work around this by providing a functor which provides a deleter which does not require the complete type on declaration:
template <typename T>
void custom_deleter(T* param) {
delete param;
}
To use custom_deleter
as a template parameter we'll need to make it into a functor otherwise the compiler will error:
error C2207:
std::_Unique_ptr_base<_Ty,_Dx,_Empty_deleter>::_Mydel
: a member of a class template cannot acquire a function type
So in the header where Foo
is forward declared we'll need to define the unique_ptr
as:
unique_ptr<Foo, function<void(Foo*)>> pFoo
And in the implementation file where Foo
is defined we'll need to assign it as:
pFoo = decltype(pFoo)(new Foo, std::function<void(Foo*)>(custom_deleter<Foo>))
来源:https://stackoverflow.com/questions/57992892/workaround-for-making-a-unique-ptr-to-a-forward-declared-type