C++ STL 容器不允许使用不完整类型进行实例化;这是未定义的行为。
这是绕过该限制的有效“技巧”吗?还是这个技巧仍然有未定义的行为?
#include <vector>
template<template<class, class> class Vector = std::vector>
struct my_incomplete_vector
{
struct Element;
// Element is incomplete here, but does it matter anymore?
typedef Vector<Element, std::allocator<Element> > type;
struct Element { typename type::iterator value; };
};
int main()
{
my_incomplete_vector<>::type v;
v.resize(1);
// this isn't normally possible without incomplete types
v[0].value = v.begin();
return 0;
}