{"id":3691,"date":"2025-05-04T17:29:36","date_gmt":"2025-05-04T11:59:36","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3691"},"modified":"2025-05-04T17:29:36","modified_gmt":"2025-05-04T11:59:36","slug":"beyond-ownership-understanding-the-role-of-stdweak_ptr-in-modern-c","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2025\/05\/04\/beyond-ownership-understanding-the-role-of-stdweak_ptr-in-modern-c\/","title":{"rendered":"Beyond Ownership: Understanding the Role of std::weak_ptr in Modern C++"},"content":{"rendered":"<p><em><strong>Smart Pointers<\/strong><\/em> in <em><strong>C++<\/strong><\/em> are elegant abstractions for managing dynamic memory safely, avoiding dangling pointers and preventing leaks. While <em><strong>std::unique_ptr<\/strong><\/em> and <em><strong>std::shared_ptr<\/strong><\/em> are well understood and widely used, <em><strong>std::weak_ptr<\/strong><\/em> often demands a deeper dive to use correctly. In this article, I will attempt to explain what it is and where it is useful.<\/p>\n<p>Some basics first. <em><strong>unique_ptr<\/strong><\/em> is useful when we need exclusive ownership of a resource. It cannot be copied, only moved. On the otherhand, we use <em><strong>shared_ptr<\/strong><\/em> when we need shared ownership. Internally it uses reference counting to keep track of shared references.<\/p>\n<p><em><strong>weak_ptr<\/strong><\/em> is different from the above two. At its core, it is a smart pointer that holds a <em><strong>non-owning (\u201cweak\u201d)<\/strong><\/em> reference to an object managed by <em><strong>shared_ptr<\/strong><\/em>. The key point to note is that it doesn\u2019t <em><strong>\u201cown\u201d<\/strong><\/em> anything, but can observe whether the object still exists.<\/p>\n<p>Let us start with a simple example. Assume we have two classes that need to reference each other. See the code below:<\/p>\n<figure id=\"attachment_3692\" aria-describedby=\"caption-attachment-3692\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3692\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2025\/05\/04\/beyond-ownership-understanding-the-role-of-stdweak_ptr-in-modern-c\/code1-14\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code1.jpg\" data-orig-size=\"798,768\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1746285372&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Circular Reference Example\" data-image-description=\"&lt;p&gt;Circular Reference Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Circular Reference Example&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code1-300x289.jpg\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code1.jpg\" class=\"wp-image-3692\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code1.jpg?resize=500%2C481&#038;ssl=1\" alt=\"Circular Reference Example\" width=\"500\" height=\"481\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code1.jpg?resize=300%2C289&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code1.jpg?resize=768%2C739&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code1.jpg?w=798&amp;ssl=1 798w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3692\" class=\"wp-caption-text\"><strong>Circular Reference Example<\/strong><\/figcaption><\/figure>\n<p>In the above case, both are using <em><strong>shared_ptr<\/strong><\/em> to reference each other. The problem with this is that it results in a <em><strong>\u201ccyclic reference\u201d<\/strong><\/em> and hence the objects will not be destroyed! This is confirmed by the program output:<\/p>\n<figure id=\"attachment_3693\" aria-describedby=\"caption-attachment-3693\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3693\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2025\/05\/04\/beyond-ownership-understanding-the-role-of-stdweak_ptr-in-modern-c\/output1-19\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output1.jpg\" data-orig-size=\"777,117\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1746285444&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output1-300x45.jpg\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output1.jpg\" class=\"wp-image-3693\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output1.jpg?resize=450%2C68&#038;ssl=1\" alt=\"Program Output\" width=\"450\" height=\"68\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output1.jpg?resize=300%2C45&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output1.jpg?resize=768%2C116&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output1.jpg?w=777&amp;ssl=1 777w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-3693\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>You can see that the <em><strong>destructors<\/strong><\/em> are not called.<\/p>\n<p>The correct solution is to use a <em><strong>weak_ptr<\/strong><\/em> in one of the classes, say <em><strong>\u201cStructB\u201d<\/strong><\/em>. The changed version is here:<\/p>\n<figure id=\"attachment_3694\" aria-describedby=\"caption-attachment-3694\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3694\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2025\/05\/04\/beyond-ownership-understanding-the-role-of-stdweak_ptr-in-modern-c\/code2-17\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code2.jpg\" data-orig-size=\"791,228\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1746285500&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Using weak_ptr Instead\" data-image-description=\"&lt;p&gt;Using weak_ptr Instead&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using weak_ptr Instead&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code2-300x86.jpg\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code2.jpg\" class=\"wp-image-3694\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code2.jpg?resize=450%2C130&#038;ssl=1\" alt=\"Using weak_ptr Instead\" width=\"450\" height=\"130\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code2.jpg?resize=300%2C86&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code2.jpg?resize=768%2C221&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code2.jpg?w=791&amp;ssl=1 791w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-3694\" class=\"wp-caption-text\"><strong>Using weak_ptr Instead<\/strong><\/figcaption><\/figure>\n<p>The first thing to note is that a <em><strong>weak_ptr<\/strong><\/em> is created from a <em><strong>shared_ptr<\/strong><\/em>, not from a raw pointer. In this case, since the <em><strong>weak_ptr<\/strong><\/em> doesn\u2019t <em><strong>\u201cown\u201d<\/strong><\/em> the reference, both the objects are correctly destroyed. See the output:<\/p>\n<figure id=\"attachment_3695\" aria-describedby=\"caption-attachment-3695\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3695\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2025\/05\/04\/beyond-ownership-understanding-the-role-of-stdweak_ptr-in-modern-c\/output2-22\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output2.jpg\" data-orig-size=\"773,161\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Output After Correction\" data-image-description=\"&lt;p&gt;Output After Correction&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output After Correction&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output2-300x62.jpg\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output2.jpg\" class=\"wp-image-3695\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output2.jpg?resize=450%2C94&#038;ssl=1\" alt=\"Output After Correction\" width=\"450\" height=\"94\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output2.jpg?resize=300%2C62&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output2.jpg?resize=768%2C160&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output2.jpg?w=773&amp;ssl=1 773w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-3695\" class=\"wp-caption-text\"><strong>Output After Correction<\/strong><\/figcaption><\/figure>\n<p>The following example shows other interesting aspects of <em><strong>weak_ptr<\/strong><\/em>:<\/p>\n<figure id=\"attachment_3696\" aria-describedby=\"caption-attachment-3696\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3696\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2025\/05\/04\/beyond-ownership-understanding-the-role-of-stdweak_ptr-in-modern-c\/code3-9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code3.jpg\" data-orig-size=\"1181,1055\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1746299669&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Other Features of weak_ptr\" data-image-description=\"&lt;p&gt;Other Features of weak_ptr&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Other Features of weak_ptr&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code3-300x268.jpg\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code3-1024x915.jpg\" class=\"wp-image-3696\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code3.jpg?resize=600%2C536&#038;ssl=1\" alt=\"Other Features of weak_ptr\" width=\"600\" height=\"536\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code3.jpg?resize=300%2C268&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code3.jpg?resize=1024%2C915&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code3.jpg?resize=768%2C686&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code3.jpg?w=1181&amp;ssl=1 1181w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-3696\" class=\"wp-caption-text\"><strong>Other Features of weak_ptr<\/strong><\/figcaption><\/figure>\n<p>Here are the key points to understand from the above example:<\/p>\n<blockquote>\n<p style=\"padding-left: 40px;\">1) It is OK to initialize a <em><strong>week_ptr<\/strong><\/em> with another <em><strong>weak_ptr<\/strong><\/em>, but this doesn\u2019t change the reference count of the managed object.<\/p>\n<p style=\"padding-left: 40px;\">2) The <em><strong>\u201cuse_count()\u201d<\/strong><\/em> function returns the reference count of the <em><strong>shared_ptr<\/strong><\/em> referenced by this <em><strong>weak_ptr<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p style=\"padding-left: 40px;\">3) The <em><strong>\u201clock()\u201d<\/strong><\/em> method is required to access the managed object. This will be empty if the <em><strong>shared_ptr<\/strong><\/em> has been destructed.<\/p>\n<p style=\"padding-left: 40px;\">4) The <em><strong>\u201cexpired\u201d<\/strong><\/em> method returns <em><strong>\u201ctrue\u201d<\/strong><\/em> if the managed object has been deleted, else it returns <em><strong>\u201cfalse\u201d<\/strong><\/em>.<\/p>\n<\/blockquote>\n<p>Let us conclude this article with an example of the popular <em><strong>\u201cObserver\u201d<\/strong><\/em> pattern:<\/p>\n<figure id=\"attachment_3697\" aria-describedby=\"caption-attachment-3697\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3697\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2025\/05\/04\/beyond-ownership-understanding-the-role-of-stdweak_ptr-in-modern-c\/code4-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code4.jpg\" data-orig-size=\"913,1138\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1746349942&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Observer Pattern\" data-image-description=\"&lt;p&gt;Observer Pattern&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Observer Pattern&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code4-241x300.jpg\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code4-822x1024.jpg\" class=\"wp-image-3697\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code4.jpg?resize=500%2C623&#038;ssl=1\" alt=\"Observer Pattern\" width=\"500\" height=\"623\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code4.jpg?resize=241%2C300&amp;ssl=1 241w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code4.jpg?resize=822%2C1024&amp;ssl=1 822w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code4.jpg?resize=768%2C957&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code4.jpg?w=913&amp;ssl=1 913w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3697\" class=\"wp-caption-text\"><strong>Observer Patte<\/strong>rn<\/figcaption><\/figure>\n<p>The <em><strong>EventDispatcher<\/strong><\/em> class handles registering and notifying multiple listerners. To keep things simple, the <em><strong>\u201clistener\u201d<\/strong><\/em> is implemented as a <em><strong>\u201cfunction\u201d<\/strong> <\/em>object, but it could be a more feature-rich class. Note how the <em><strong>\u201cnotify()\u201d<\/strong><\/em> method removes deleted listeners from the queue.<\/p>\n<p>When we run the program, this is the output:<\/p>\n<figure id=\"attachment_3698\" aria-describedby=\"caption-attachment-3698\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3698\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2025\/05\/04\/beyond-ownership-understanding-the-role-of-stdweak_ptr-in-modern-c\/output4-8\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output4.jpg\" data-orig-size=\"782,164\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1746350064&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Observer Pattern Output\" data-image-description=\"&lt;p&gt;Observer Pattern Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Observer Pattern Output&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output4-300x63.jpg\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output4.jpg\" class=\"wp-image-3698\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output4.jpg?resize=450%2C94&#038;ssl=1\" alt=\"Observer Pattern Output\" width=\"450\" height=\"94\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output4.jpg?resize=300%2C63&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output4.jpg?resize=768%2C161&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/output4.jpg?w=782&amp;ssl=1 782w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-3698\" class=\"wp-caption-text\"><strong>Observer Pattern Output<\/strong><\/figcaption><\/figure>\n<p>In summary, here are the key properties of <em><strong>weak_ptr<\/strong><\/em>:<\/p>\n<blockquote><p>1. <strong>Non-ownership:<\/strong> It doesn&#8217;t own the resource it references and hence doesn&#8217;t affect its lifetime<\/p>\n<p>2. <strong>Observation capability:<\/strong> It can observe whether the object still exists<\/p>\n<p>3. <strong>Convertibility:<\/strong> It can be converted to a shared_ptr to access the object (if it still exists)<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>4. <strong>Safety:<\/strong> It helps in preventing dangling pointer issues by providing safe access mechanisms<\/p><\/blockquote>\n<p>Hope you found the article useful. You can download the source from <a href=\"https:\/\/www.rangakrish.com\/downloads\/WeakPtrExample1.cpp\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>. The code was tested in <em><strong>Visual Studio Professional (64 bit) ver 17.13.5<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Have a great week ahead!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Smart Pointers in C++ are elegant abstractions for managing dynamic memory safely, avoiding dangling pointers and preventing leaks. While std::unique_ptr and std::shared_ptr are well understood and widely used, std::weak_ptr often demands a deeper dive to use correctly. In this article, I will attempt to explain what it is and where it is useful. Some basics [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[49,17],"tags":[371,423,421,422,424],"class_list":["post-3691","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c20","tag-shared_ptr","tag-smart-pointer","tag-unique_ptr","tag-weak_ptr"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-Xx","jetpack-related-posts":[{"id":2889,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/","url_meta":{"origin":3691,"position":0},"title":"std::array vs. std::vector","author":"admin","date":"August 9, 2022","format":false,"excerpt":"One of the common questions when starting to study the container abstractions in C++20 is \"Should I use std::array\u00a0or std::vector?\". The correct answer is \"use what is best suited for the current situation\". That is not good enough, obviously. In this short article, I will try to answer this question\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"std::array Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2876,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/07\/27\/understanding-stdspan\/","url_meta":{"origin":3691,"position":1},"title":"Understanding std::span","author":"admin","date":"July 27, 2022","format":false,"excerpt":"Introduced in C++20, std::span\u00a0is a light-weight abstraction that provides a convenient view\u00a0into a collection of contiguous elements. Note that it is not enough for the elements to be logically contiguous, but they must be contiguous in memory too. Thus, span will work with C-style arrays, C++ vectors and arrays. It\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example-1: span","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code1-285x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1091,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/","url_meta":{"origin":3691,"position":2},"title":"C++17 &#8211; std::apply() and std::invoke()","author":"admin","date":"October 14, 2018","format":false,"excerpt":"Calling a function (or function object) dynamically, through a pointer known at runtime, is a common programming scenario. Almost all languages support this use case. Lisp, for example, has apply and funcall. When using apply, you can see that the arguments are passed via a separate list object. With funcall,\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Calling Member Functions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1905,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/","url_meta":{"origin":3691,"position":3},"title":"Deleted Destructor in C++","author":"admin","date":"March 4, 2020","format":false,"excerpt":"Since C++ 11, we can use the keyword delete to declare functions as deleted and thus prohibit the use of these functions in the code. See this article\u00a0for a brief overview. In particular, the destructor of a class\/struct\/union can be declared as deleted. In today\u2019s article, I am going to\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example-8","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-8.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":683,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/","url_meta":{"origin":3691,"position":4},"title":"C++17: std::variant","author":"admin","date":"November 5, 2017","format":false,"excerpt":"C++17 introduces a new type-safe union in the form of std::variant. At any time, it can store a single value from one of many types. We need to include <variant>\u00a0to use this feature. Let us look at a simple example to get started: First, we define a variant object v1\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":700,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/","url_meta":{"origin":3691,"position":5},"title":"C++17: std::optional","author":"admin","date":"November 21, 2017","format":false,"excerpt":"Suppose we want to write a function that returns a value, but with the possibility that\u00a0 the computation might fail. This failure can be represented as an exception, or as a return value that unamibiguously denotes failure (for example, -1). Throwing an exception is a strong form of failure and\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=3691"}],"version-history":[{"count":4,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3691\/revisions"}],"predecessor-version":[{"id":3702,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3691\/revisions\/3702"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}