C++11: unique_ptr complains about incomplete type, but not when I wrap it
There are already a good number of questions about unique_ptr and
incomplete type on SO, but none can give me a concept in understanding why
the following does not work:
// error: ... std::pair<...>::second has incomplete type
template<typename K, typename T> struct Impl {
typedef typename std::unordered_map<K,Impl<K,T>>::iterator iter_type;
std::unique_ptr<iter_type> ptr;
Impl() : ptr(new iter_type()) {}
};
int main() { Impl<int,int>(); return 0; }
while the following does:
template<typename K, typename T> struct Impl {
struct Wrapper {
typedef typename std::unordered_map<K,Impl<K,T>>::iterator iter_type;
iter_type iter;
};
std::unique_ptr<Wrapper> ptr;
Impl() : ptr(new Wrapper()) {}
};
int main() { Impl<int,int>(); return 0; }
I don't see where the technical difference is: If std::pair<...>::second
(that is, Impl<K,T>) was incomplete to Impl in the first example, it
should be incomplete to Wrapper in the second one as well. Also, when it
suffices to wrap the unique_ptr in a struct, why is there a restriction
for the first case?
EDIT:
It seems it has something to do with the fact that I'm not explicitly
constructing iter_type in the second case. But I don't know how to do
aswell in the first case, and what should be the difference to the
compiler.
No comments:
Post a Comment