问题
Answering this question, I got a surprising error from GCC and Clang:
template< typename = void >
struct Outer_temp
{
struct Inner;
Inner myinner;
};
template<>
struct Outer_temp<void>::Inner // Error: specialization of Outer_temp
{ // with member myinner of incomplete type.
};
Why does declaring an explicit specialization require implicit instantiation? Does this fall into the same category as all uses of the scope resolution operator?
Instantiating the template to find the declaration of a member object I would understand, but in the case of a member class, you can check that the member exists and is a class without instantiating anything. (You do need partial specialization resolution though.)
回答1:
On reflection, this is a flaw inherent to the language design and a quirk of explicit specialization. There is no way to match the explicit specialization declaration to the member declaration without instantiating the class template, and all its member declarations.
I will not submit a defect report because the use case is too minor to worry about, and it won't bite someone unaware because current implementations diagnose the condition.
来源:https://stackoverflow.com/questions/20110225/explicit-specialization-incurs-instantiation