为什么 class 模板在不需要完全对象类型时被实例化?

Why is a class template instantiated when it's ot required to be a completely object type?

Class 模板可以显式或隐式实例化,如果 N3797::14.7.1/1 [temp.inst]

则 class 模板被隐式实例化

Unless a class template specialization has been explicitly instantiated (14.7.2) or explicitly specialized (14.7.3), the class template specialization is implicitly instantiated when the specialization is referenced in a context that requires a completely-defined object type or when the completeness of the class type affects the semantics of the program.

让我提供一个例子,当上下文不需要 class 类型被完全定义时:

#include <iostream>

template<class T>
struct A
{
    void foo();
};

template<class T> void A<T>::foo(){ std::cout << "foo" << std::endl; }

A<int>* a;
int main(){ a -> foo(); }

DEMO

在该示例中,class 模板既未显式实例化也未隐式实例化。所以,我们实际上没有classA<int>的定义。但尽管它工作正常。你不能解释这种行为吗?

请参阅 14.7.1/4 中的示例,该示例演示 a -> foo(); 是 "a context that requires a completely-defined object type"。

[expr.ref]/p2,强调我的:

For the second option (arrow) the first expression shall have pointer to complete class type.

为什么你认为它是一个不需要完全定义的对象类型的上下文?