考虑以下代码:
struct A; // incomplete type
template<class T>
struct D { T d; };
template <class T>
struct B { int * p = nullptr; };
int main() {
B<D<A>> u, v;
u = v; // doesn't compile; complain that D<A>::d has incomplete type
u.operator=(v); // compiles
}
演示。由于u.operator=(v)
可以编译,但u = v;
不能,因此在后者表达式的重载决议过程中,编译器必须隐式实例化D<A>
- 但我不明白为什么需要这个实例化。为了使事情更有趣,这段代码可以编译:
struct A; // incomplete type
template<class T>
struct D; // undefined
template <class T>
struct B { int * p = nullptr; };
int main() {
B<D<A>> u, v;
u = v;
u.operator=(v);
}
演示.
这里发生了什么?为什么u = v;
会导致在第一个例子中隐式实例化D<A>
- 一个在B
的定义体中根本没有使用过的类型,而在第二个例子中却不会呢?
p
的初始化器,以防万一;但是没有任何改变。(而且VS的模板引擎通常是非标准的,所以...) - T.C.A
没有被使用,你不需要知道它的大小) - KugBuBu