1

問題のコード:

template<typename T>
class SharedObject {
 public:
  typedef boost::intrusive_ptr<T> Pointer;
  typedef boost::intrusive_ptr<T const> ConstPointer;
  inline Pointer GetPointer() {
    return Pointer(this); //Ambiguous call here
  }
  inline ConstPointer GetPointer() const {
    return ConstPointer(this);
  }
  ...

次のように使用します。

template <typename T>
class SomeClass: public SharedObject<SomeClass<T> > {
 public:
  static inline boost::intrusive_ptr<SomeClass<T> > Create() {
    return (new SomeClass)->GetPointer();
  }
};

int main()
{
  auto v = SomeClass<int>::Create();
}

GCC (4.4.1) でブースト 1.41 を使用すると、GetPointer() の最初の (非 const) バージョンを開始すると、次のエラーが発生します。

error: call of overloaded ‘intrusive_ptr SharedObject<SomeClass<int> >* const)’ is ambiguous
boost/smart_ptr/intrusive_ptr.hpp:118: note: candidates are: boost::intrusive_ptr<T>::intrusive_ptr(boost::intrusive_ptr<T>&&) [with T = SomeClass<int>] <near match>
boost/smart_ptr/intrusive_ptr.hpp:94:  note:                 boost::intrusive_ptr<T>::intrusive_ptr(const boost::intrusive_ptr<T>&) [with T = SomeClass<int>] <near match>
boost/smart_ptr/intrusive_ptr.hpp:70:  note:                 boost::intrusive_ptr<T>::intrusive_ptr(T*, bool) [with T = SomeClass<int>] <near match>

私の C++ の難解なスキルには、あいまいさがある理由がまったくわかりません。188 行目と 94 行目の 2 つの候補は、既存の intrusive_ptr 右辺値参照を使用していますが、これSharedObject::thisは確かにそうではありません。ただし、最終候補は完全に一致します (bool 引数はオプションです)。

問題が何であるかについて私に教えてくれる人はいますか?

EDIT +回答:私はついにそれを実現しました

  inline Pointer GetPointer() {
    return Pointer(this); //Ambiguous call here
  }

thisPointer typedef が SomeClass である間、SharedObject を参照します。(これは、Butterworth がすぐに指摘したことのほとんどです)。

  inline Pointer GetPointer() {
    return Pointer(static_cast<C*>(this));
  }

私はthis本当に SomeClass であり、SharedObject から継承していることを知っているので、static_cast はテンプレート クラスを一巡させます。

4

1 に答える 1

2

あなたが言う時:

typedef boost::intrusive_ptr<T> Pointer;

テンプレートがコードでインスタンス化されるときに、(その時点でがであるintため)への侵入型ポインターである型を宣言しています。SharedObject クラスは ではないため、 を使用してそのような侵入ポインターをインスタンス化することはできません。Tintintthis

編集:わかりました、コードを誤解しました。もう一度やり直します。で:

return Pointer(this); //Ambiguous call here

エラーメッセージによると、これは SharedObject ですが、ポインターは SomeClass に型定義されていると思います。

あなたのコードは信じられないほど理解しにくいものです。何をしようとしても、もっと簡単な方法があるはずです。また、基本クラスに仮想デストラクタ (およびおそらく仮想関数) がないようです。

于 2010-01-22T10:26:56.723 に答える