質問

ネストされたテンプレートと代入演算子のオーバーライドに関する問題があります。 refcountingクラステンプレート_referenceが必要だとします。この_referenceは今のところ単純に 参照カウントされたオブジェクトへのポインタを保持します。問題は、これがすべて正常に機能することです。 単純なクラスまたは構造体でこれを行う限り。例えば。 _reference ...、

しかし、今では、保持しているクラスを転送するstd-vectorへの参照であるクラステンプレートを作成します。

はい、私はコードを投稿するだけです:(それは、今のところ参照カウントとそのようなことをしません、それはちょうど私が持っている問題の抽出です)

template <typename T>
class _reference
{
private:
    T* p_;

public:

// !!! this assignment seems only to work, when T is no class template already...
void operator= (T* r)                   
{
    p_ = r;
}

// WHILE this ALWAYS works as well...
void simplySetIt (T* r)                 
{
    p_ = r;
}
};

template <typename T>
class _ref_vector : public _reference<vector<T> >
{
};

void test2 ()
{
_reference<vector<long> > ref_ptr2;
_ref_vector<long>         ref_ptr3;

ref_ptr2 = new vector<long>;                    // works fine.

ref_ptr3 = new vector<long>;                // BUT: THIS doesnt work
    ref_ptr3.simplySetIt (new vector<long>);    // WHILE: this works fine...
}

MSVC-Error:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 
'std::vector<_Ty> *' (or there is no acceptable conversion)

GCCエラー:

error: no match for 'operator=' in 'ptr3 = (((const stlp_std::allocator<long int>&)
((const stlp_std::allocator<long int>*)(& stlp_std::allocator<long int>()))), 
(((stlp_std::vector<long int, stlp_std::allocator<long int> >*)operator new(12u)), 
((<anonymous> != 0u) ? (<anonymous>->stlp_std::vector<_Tp, _Alloc>::vector [with 
_Tp = long int, _Alloc = stlp_std::allocator<long int>]
(<anonymous>), <anonymous>) : <anonymous>)))'

では、simplySetIt-関数が機能するのに、なぜ代入演算子が機能しないのか、誰にでも説明してもらえますか?

役に立ちましたか?

解決

ベースoperator =は、暗黙の代入演算子によって非表示になり、オーバーロードに関与しなくなります。 _ref_vector を次のように記述する必要があります

template <typename T>
class _ref_vector : public _reference<vector<T> >
{
  using _reference<vector<T> >::operator=;
};

simplySetItのコンパイラ追加バージョンはないため、ルックアップは基本クラスでそれを見つけます。

他のヒント

標準で述べられているように(13.5.3):

コピー代入演算子operator =が暗黙的に宣言されているため ユーザーによって宣言されていない場合のクラス(12.8)、基本クラスの割り当て 演算子は、のコピー割り当て演算子によって常に非表示になります 派生クラス。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top