让我们有以下法典:
template <typename T> struct X
{ X() { }
};
struct __declspec(dllexport) A
{ struct __declspec(dllexport) AB
{ int i;
};
typedef X <AB> XAB;
//template struct __declspec(dllexport) X <AB>; // error C2252: an explicit instantiation of a template can only occur at namespace scope
static XAB x; // warning C4251: A::x : struct X<T> needs to have dll-interface to be used by clients of struct A
static X <AB> x2; // warning C4251: A::x2 : struct X<T> needs to have dll-interface to be used by clients of struct A
X <AB> x3; // warning C4251: A::x3 : struct X<T> needs to have dll-interface to be used by clients of struct A
};
template struct __declspec(dllexport) X <A::AB>; // Has no effect
因此:
- I have a class to be exported to a DLL.
- This class has a subclass to be exported to the DLL, too.
- There are variables ( x... ) of type
template <subclass>
. - The member variables need to be dll-exported, too.
- So I d need to instantiate explicitly the
template <subclass>
before this type is used. - But this is not possible as instantiation may be done only in the global scope.
But in the global scope, the subclass is not defined (yet).
The
template <class:subster>
Immediateiation after the category body has no effect.
因此,如何正确处理这一问题,以避免任何警告和错误?
BTW:在VS2008年,该类中模板的证实工作良好。
事先感谢你。
LuP