0

テンプレートクラスを使用して実装パターンへのポインタを実装しようとしています。独立したクラスとその実装を作成するために、次のアプローチを使用します。

template<typename T>
struct __A_impl;

template<typename T>
struct A
{
    using __impl_t = __A_impl<T>;

    A(__impl_t* t);

    __impl_t* _t;
};

template<typename T>
struct B
{
    T _t;
};

template<typename T>
using __A_impl = B<T>;

このフォームでは、「B」の名前を変更したい場合、これはAの定義にはまったく影響しません。しかし、gccで次のエラーが発生します。

test.cpp:21:22: error: declaration of template 'template<class T> using __A_impl =    B<T>'
test.cpp:2:7: error: conflicts with previous declaration 'template<class T> class __A_impl'
test.cpp:21:22: error: redeclaration of 'template<class T> using __A_impl = B<T>'
test.cpp:2:7: note: previous declaration 'template<class T> class __A_impl'

どうすればこれを達成できますか?typedef宣言者では不可能だからです。

4

2 に答える 2

2

typedefを前方宣言することはできません。クラス/構造体/列挙型のみを前方宣言できます。

また、PIMPLは通常、コンパイラーが実装を認識しないようにするために使用されますが、あなたの場合、それはテンプレートであるため、決して起こらないので、要点はわかりません。

于 2013-01-27T11:30:31.017 に答える
0

まあ、これはコンパイルされますが、あなたが望むものではないかもしれません:

template<typename T>
struct B
{
    T _t;
};

template<typename T>
using __A_impl = B<T>;

template<typename T>
struct A
{
    using __impl_t = __A_impl<T>;

    A(__impl_t* t);

    __impl_t* _t;
};

とにかく、2つのこと:

  1. 使ってみませんstd::unique_ptrか?
  2. 二重下線は実装用に予約されています。それらを使用しないでください。
于 2013-01-27T11:28:15.853 に答える