Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 789459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:31:43+00:00 2026-05-14T21:31:43+00:00

I recently tried building my own shared and weak pointers. Code that compiles using

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:31:43+00:00Added an answer on May 14, 2026 at 9:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 428k
  • Answers 428k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Replace <div style="-moz-user-select: none;"> with <div style="-moz-user-select: -moz-none;"> The description… May 15, 2026 at 1:20 pm
  • Editorial Team
    Editorial Team added an answer I've used Informa, but your problem has nothing to do… May 15, 2026 at 1:20 pm
  • Editorial Team
    Editorial Team added an answer You can have your instances raise an event (OnInactivated, say).… May 15, 2026 at 1:20 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.