C++ - 使用GCC编译时,'operator ='不匹配

Yip*_*Yay 3 c++ compiler-construction gcc no-match operator-keyword

我最近尝试构建自己的共享和弱指针.使用Visual Studio编译的代码无法在GCC(4.5.0)中编译,但出现以下错误:

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>]
Run Code Online (Sandbox Code Playgroud)

以下是我的代码中最重要的部分:

1)弱指针实现(注意声明operator=)

#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);
   }
};
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

问:

为什么会这样?我可能很愚蠢,但我看不出这个代码有什么问题,也无法解决GCC行为.如果有人可以解释为什么这个代码编译以及为什么它在MSVS下工作(我的意思是,为什么一个编译器会做得很好以及为什么第二个失败)我也会感激.谢谢.

更新:完整的代码和编译器错误可以在这里看到 - http://codepad.org/MirlNayf

Tho*_*mas 5

您的赋值运算符需要引用,而不是const引用:

weak_ptr& operator= (weak_ptr& p)
Run Code Online (Sandbox Code Playgroud)

但是,表达式weak_ptr<int>(sp1)导致临时,只能转换为const引用,因为它是一个rvalue.这样考虑一下:你不能修改表达式的结果,但你的赋值运算符要求它可以.

解决方案是将这样的赋值运算符声明为:

weak_ptr& operator= (const weak_ptr& p)
Run Code Online (Sandbox Code Playgroud)

为什么VC++接受这个是超出我的...也许你应该启用一些标准合规性标志.