문제

중첩 된 템플릿과 할당 연산자의 재정의 문제가 있습니다. refcounting 클래스 템플릿 _reference를 원한다고 가정 해 봅시다. 이 _reference는 현재 REF 카운트 객체에 대한 포인터를 보유합니다. 문제는 이제 간단한 클래스 나 구조로 이것을 수행하는 한이 모든 것이 잘 작동한다는 것입니다. 예를 들어. _참조 ...,

그러나 이제는 보유한 클래스를 전달하는 STD 벡터에 대한 참조 인 클래스 템플릿을 만들고 싶습니다.

아니, 나는 단지 코드를 게시한다.

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 :

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>)))'

따라서 할당 연산자가 왜 여기서 작동하지 않는지 설명 할 수 있는데, 단순히 설정 - 기능은 왜 나에게 설명 할 수 있습니까?

도움이 되었습니까?

해결책

기본 연산자 = 암시 적 할당 연산자에 의해 숨겨져 더 이상 과부하에 참여하지 않도록합니다. 당신은 써야합니다 _ref_vector ~처럼

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

간단히 세트 트리트의 컴파일러 에드 버전이 없으므로 조회는 기본 클래스에서 찾을 수 있습니다.

다른 팁

표준이 말한 것처럼 (13.5.3) :

사용자가 선언하지 않은 경우 (12.8) 클래스에 대해 복사 할당 연산자 연산자 =가 암시 적으로 선언되므로, 기본 클래스 할당 연산자는 항상 파생 클래스의 사본 할당 연산자에 의해 숨겨져 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top