2

次のコードを作成しましょう。

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

それで:

  • DLLにエクスポートするクラスがあります。
  • このクラスには、DLLにエクスポートするサブクラスもあります。
  • タイプの変数('x ...')がありtemplate <subclass>ます。
  • メンバー変数もdllエクスポートする必要があります。
  • したがって、template <subclass>このタイプを使用する前に、明示的にインスタンス化する必要があります。
  • ただし、インスタンス化はグローバルスコープでのみ実行できるため、これは不可能です。
  • ただし、グローバルスコープでは、サブクラスは(まだ)定義されていません。

  • template <class::subclass>クラス本体の後のインスタンス化は効果がありません。

では、警告やエラーを回避するために、この問題を正しく処理するにはどうすればよいでしょうか。

ところで:クラス内のテンプレートのインスタンス化はVS2008でうまく機能しました。

前もって感謝します。

LuP

4

0 に答える 0