views:

192

answers:

2

Hello! I recently tried building my own shared and weak pointers. Code that compiles using Visual Studio doesn't compile in GCC (4.5.0) with the following error:

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>]

Here are the most important parts of my code:

1) Weak pointer implementation (note the declaration of 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);
   }
};

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;
}

Q:

Why does this happen? I'm probably plain stupid, but I can't see what's wrong with this code and can't undestand GCC behaviour. I would also appreciate if someone could explain why does this code compile and why does it work under MSVS (I mean, why would one compiler do it fine and why would the second fail). Thank you.

Update: Full code and compiler error can be seen here - http://codepad.org/MirlNayf

+3  A: 

Your assignment operator requires a reference, not a const reference:

weak_ptr& operator= (weak_ptr& p)

However, the expression weak_ptr<int>(sp1) results in a temporary, which can only be converted to a const reference, since it is an rvalue. Think about it this way: you cannot modify the result of an expression, yet your assignment operator requires that it can.

The solution is to declare your assignment operator like this instead:

weak_ptr& operator= (const weak_ptr& p)

Why VC++ accepts this is beyond me... maybe you should enable some standards compliance flags.

Thomas
MS calls the fact that VC binds rvalues to non-`const` references an "extension". I think it's a bug.
sbi
A: 

It is because you first constructed a new object from sp1, then assigned it, which is not your intended behaviour I expect. However, the fundamental error is because assignment should take const references.

DeadMG