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 6184969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:37:53+00:00 2026-05-24T01:37:53+00:00

The enable_shared_from_this helper contains a weak pointer that is set when creating shared pointer

  • 0

The enable_shared_from_this helper contains a weak pointer that is set when creating shared pointer to the object. That means there is the reference-count (allocated separately or together with the object using make_shared) and an extra weak_ptr in the object.

Now why doesn’t it simply contain the reference count instead? When setting shared_ptr from dumb pointer, the type has to be fully defined, so the shared_ptr constructor or assignment operator can detect the type is derived from enable_shared_from_this and use the correct counter and the format can remain the same, so copying does not care. In fact, the shared_ptr already has to detect it to set the embedded weak_ptr.

  • 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-24T01:37:54+00:00Added an answer on May 24, 2026 at 1:37 am

    The first thing that comes to mind is whether that approach would be feasible at all, and the answer is that it would not:

    struct X : enable_shared_from_this {};
    std::shared_ptr<X> p( new X );
    std::weak_ptr<X> w( p );
    p.reset();                      // this deletes the object
    if ( w.use_count() ) {          // this needs access to the count object
                                    //    but it is gone! Undefined Behavior
    

    If the count is stored in the object, then no weak_ptr can outlive the object, which is a breach in the contract. The whole idea of weak_ptr is that they can outlive the object (if the last shared_ptr goes out of scope, the object is deleted even if there are weak_ptr)

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

Sidebar

Related Questions

I'm using boost's shared pointers, and enable_shared_from_this to enable returning a shared pointer to
I have an shared object that need to be send to a system API
Is there a way to prevent shared_from_this() call for a stack-allocated object ? The
I have a class that derives from enable_shared_from_this ... (Recently been added to std
I built wxWidgets on Linux using this command: ../configure --enable-shared --disable-debug It see results
I'm looking for independent implementation of boost/tr1 shared_ptr , weak_ptr and enable_shared_from_this . I
Imagine the following situation: class IAlarm : public boost::enable_shared_from_this<IAlarm> { boost::shared_ptr<IAlarm> getThisPointerForIAlarm() { return
I want to create an async. server. i succeed to do that, but now
I'm using enable_shared_from_this<Base> and then inherit from Base . When trying to use shared_from_this()
I'm using enable_shared_from_this<Base> and then inherit from Base . When trying to use shared_from_this()

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.