开发者

Nested class, dllexport and VS2010

开发者 https://www.devze.com 2023-04-10 19:18 出处:网络
Let\'s have the following code: template <typename T> struct X { X() { } }; struct __declspec(dllexport) A

Let's have the following code:

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 exp开发者_如何学Golicit 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

So:

  • 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::subclass> instantiation after the class body has no effect.

So how to handle this problem correctly, to avoid any warnings and errors?

BTW: Instantiation of template within the class worked well in VS2008.

Thank you in advance.

LuP

0

精彩评论

暂无评论...
验证码 换一张
取 消