2010-05-20 2 views
3

최근에 공유 및 약한 포인터를 작성하려고했습니다. 다음 오류 (4.5.0) GCC에서 컴파일되지 않습니다 비주얼 스튜디오를 사용하여 컴파일 코드 :C++ - GCC를 사용하여 컴파일 할 때 'operator ='와 일치하지 않습니다.

다음
main.cpp: In function 'int main()': 
main.cpp:18:27: error: no match for 'operator=' in 'wp1 = weak_ptr<int>(((const shared_ptr<int>&)((const shared_ptr<int>*)(& sp1))))' 
weak_ptr.h:59:9: note: candidate is: void weak_ptr<T>::operator=(weak_ptr<T>&) [with T = int, weak_ptr<T> = weak_ptr<int>] 

내 코드의 가장 중요한 부분입니다 :

1) 약한 포인터 구현

#include "smart_ptr_wrapper.hpp" 
#include "shared_ptr.h" 

template <typename T> 
class weak_ptr { 
private: 
    // Weak wrapper implementation 
    typedef smart_ptr_wrapper<T> weak_ptr_wrapper; 
    weak_ptr_wrapper* wrapper; 

private: 
    // Shared wrapper additional routines 
    void increase_reference_count() { 
     ++(wrapper->weak_count); 
    } 
    void decrease_reference_count() { 
     --(wrapper->weak_count); 

     // Dispose the wrapper if there are no more 
     // references to this object 
     // @note This should actually lock the wrapper to 
     // preserve thread safety 
     if (wrapper->strong_count == 0 && wrapper->weak_count == 0) { 
     delete wrapper; 
     } 
    } 

public: 
    // Default constructor to grant syntax flexibility 
    weak_ptr() : wrapper(NULL) { } 

    weak_ptr(const shared_ptr<T>& pointer) : wrapper(pointer.wrapper) { 
     increase_reference_count(); 
    } 

    weak_ptr(const weak_ptr& p) : wrapper(p.wrapper) { 
     increase_reference_count(); 
    } 

    weak_ptr& operator= (weak_ptr& p) { 
     // Set new reference counts 
     // @note If this is 'just-a-pointer', which was created 
     // using default constructor then our wrapper would be 'NULL' 
     if (wrapper != NULL) { 
     decrease_reference_count(); 
     } 
     p.increase_reference_count(); 
     // Set new wrapper 
     wrapper = p.wrapper; 

     return *this; 
    } 

    ~weak_ptr() { 
     decrease_reference_count(); 
    } 

    T* get() const { return (wrapper->strong_count == 0) ? NULL: wrapper->raw_pointer; } 
    T* operator->() const { return get(); } 
    T& operator* () const { return *get(); } 

    // User comparison operation 
    operator void*() const { 
     return (get() == NULL); 
    } 
}; 

2) MAIN.CPP

int main() { 
    shared_ptr<int> sp1(new int(4)); 
    weak_ptr<int> wp1(sp1); 
    // Next line can't be compiled by gcc... Why? 
    wp1 = weak_ptr<int>(sp1); 
    return 0; 
} 
,536,913 ( operator= 선언 참고)

Q :

왜 이런 일이 발생합니까? 나는 어리석은 짓 일지 모르지만이 코드가 무엇이 잘못된 것인지 알 수 없으며 GCC 동작을 알 수 없게 될 수도 없다. 누군가가이 코드를 컴파일하는 이유와 왜 MSVS에서 작동하는지 설명 할 수 있다면 감사 할 것입니다. (한 가지 컴파일러가 왜 괜찮습니까? 왜 두 번째 컴파일러가 실패할까요?) 고맙습니다.

업데이트 : 전체 코드와 컴파일러 오류가 여기에 볼 수 있습니다 - http://codepad.org/MirlNayf

+0

GCC (http://codepad.org/GUdC3D0L)에서 작동 –

+0

이전 컴파일러 확장으로 인해 MSVC++에서 작동합니다. 확장 기능을 해제하면 MSVC에서도 오류가 발생합니다. – AnT

답변

5

귀하의 할당 연산자는 참조를 필요로하지 않는 const 참조 : 임시의

weak_ptr& operator= (weak_ptr& p) 

그러나,식이 weak_ptr<int>(sp1) 결과, 이 값은 우월 값이기 때문에 const 참조로만 변환 할 수 있습니다. 이런 식으로 생각하십시오. 표현식의 결과를 수정할 수는 없지만 대입 연산자가 필요로합니다.

솔루션이 아닌처럼 할당 연산자를 선언하는 것입니다 : VC++이를 받아들이는 왜

weak_ptr& operator= (const weak_ptr& p) 

이 나를 넘어 ... 어쩌면 당신이 어떤 표준 준수 플래그를 사용하도록 설정해야합니다.

+0

MS는 VC가 rvalues를 non-'const'가 "확장자"를 참조하는 것으로 바인딩한다는 사실을 호출합니다. 나는 그것이 버그라고 생각한다. – sbi

0

sp1에서 새 개체를 만든 다음 할당했기 때문에 이는 의도 한 동작이 아니기 때문입니다. 그러나 할당은 const 참조를 사용해야하기 때문에 근본적인 오류가 발생합니다.