<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Divyansh Tripathi on Medium]]></title>
        <description><![CDATA[Stories by Divyansh Tripathi on Medium]]></description>
        <link>https://medium.com/@tdivyansh058?source=rss-1f8a063be5c4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*s9GoCMcsCXK7ro7bvg6caw.jpeg</url>
            <title>Stories by Divyansh Tripathi on Medium</title>
            <link>https://medium.com/@tdivyansh058?source=rss-1f8a063be5c4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 18:59:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tdivyansh058/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Cool C++ Features You May Not Know About!]]></title>
            <link>https://medium.com/@tdivyansh058/cool-c-features-you-may-not-know-about-11b1f8b07915?source=rss-1f8a063be5c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/11b1f8b07915</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[engineering]]></category>
            <dc:creator><![CDATA[Divyansh Tripathi]]></dc:creator>
            <pubDate>Tue, 24 Sep 2024 14:58:37 GMT</pubDate>
            <atom:updated>2024-09-24T15:02:03.448Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HfYSTd9NPUTvZ49VqmsgHg.jpeg" /></figure><p>During my recent internship, I had the opportunity to work on a C++ project and during that I stumbled upon some fascinating features and optimizations present in the language that I wasn’t aware of before. This blog is a (very shallow) list of some of these features and can serve as a casual read for someone new to C++ development(like me).</p><h4>1. Return Value Optimisation</h4><p>RVO is a C++ feature that prevents unnecessary copying of large objects when returned from a function. For example, consider the following piece of code.</p><pre>vector&lt;vector&lt;int&gt;&gt; rotate_clockwise(vector&lt;vector&lt;int&gt;&gt;&amp; initial_matrix){<br>  vector&lt;vector&lt;int&gt;&gt; temp_matrix;<br>  // perform rotation logic<br>  return temp_matrix;<br>} <br><br>void rotate_clockwise_again(<br>     vector&lt;vector&lt;int&gt;&gt;&amp; initial_matrix,<br>     vector&lt;vector&lt;int&gt;&gt;&amp; returned_matrix){<br>// perform rotation logic and assign to returned_matrix<br>}<br><br>int main() { <br>  vector&lt;vector&lt;int&gt;&gt; input_matrix = read_input();<br>  vector&lt;vector&lt;int&gt;&gt; rotated_matrix = rotate_clockwise(input_tensor);<br>  rotate_clockwise_again(input_tensor, rotated_matrix);<br>}</pre><p>At first glance, the first function seems much less efficient than the second. One might think that in the first function, we are constructing a new vector and then returning it, thus copying it to a new object (and a new memory location). However, thanks to RVO (Return Value Optimization), these two functions are approximately the same in terms of performance! The compiler detects that after the scope of the function ends, temp_matrix is no longer needed, and instead of copying it, it is directly constructed in the memory of rotated_matrix in the main function.</p><p>With this in mind, we can see that the second function is much less readable than the first, and we can avoid using such patterns!</p><h4>2. Move Semantics and std::move()</h4><p>Another cool addition to the move semantics of C++ is the std::move() function. In a sense, std::move() is not really a function in the traditional sense, but rather an indication to the compiler that the object is no longer needed and can be moved instead of copied. The best way to understand this is through a simple swapping logic.</p><pre>template&lt;typename T&gt;<br>void swap_normal(T&amp; a, T&amp; b){ <br>  T temp = b;<br>  b = a;<br>  a = temp;<br>}<br>template&lt;typename T&gt; <br>void swap_efficiently(T&amp; a, T&amp; b){ <br>  T temp = std::move(b)<br>  b = std::move(a)<br>  a = std::move(tmp)<br>}</pre><p>In the first function, we first copy object b to temp, then copy object a to b, and finally copy object temp to a. This means we perform at least three copy operations to perform a simple swap.</p><p>Now consider the second function; in this case, we do not perform any deep copies! The std::move() function allows the compiler to treat the parameter as an <a href="https://en.cppreference.com/w/cpp/language/value_category">xvalue</a>. T temp = std::move(b); moves the contents of b into temp, effectively transferring ownership of the resources without copying. After this operation, b is left in a valid but unspecified state. Similarly, we perform shallow copies in the next two assignment operations and can swap without any copies.</p><p>This may seem very similar to using pointers to do shallow copies; however, after std::move(), <strong>the original object is no longer usable</strong>.</p><p><strong>A Quick head’s up: </strong>Geared with the knowledge of RVO and std::move() one might think of writing something like this:</p><pre>vector&lt;int&gt; an_inefficient_function(vector&lt;int&gt;&amp; parameter){<br>  vector&lt;int&gt; new_vector;<br>  // some manipulation<br>  return std::move(new_vector);<br>}</pre><p>However this function is not really efficient because it prevents the compiler from optimising the code using copy elision and NRVO(Named Return Value Optimisation) which prevents unnecessary moves/copies during function calls. So, don’t do this!</p><h4>3.<strong> Smart Pointers</strong></h4><p>Managing memory and working with pointers is hard. This is especially true when you are working with pointers that are sent all over the place in a code. A pointer which is not handled correctly may create memory leaks and it would be the last thing that you would want in a production binary.</p><p>This is such a big issue that different languages have developed different ways to tackle it. There are garbage collected languages like Java, while languages like Rust have implemented their own ownership model.</p><p>In C++ smart pointers introduce explicit move semantics to deal with this issue. The main smart pointers we will discuss are std::unique_ptr and std::shared_ptr. These two are by far the most useful in practice.</p><p>a.)std::unique_ptr allows us to create a unique pointer to an object (duh). This is useful when you want to have only one pointer to any object in memory. std::unique_ptr disallows copying and can only be moved. When this pointer is passed to a function, ownership is transferred to that function. The destructor for the object is called when the owning function goes out of scope. Consider the following code to understand this:</p><pre>void callMeAFunction(std::unique_ptr&lt;Type&gt; ptr){<br>  // do some logic<br>  // ptr goes out of score and destructor for ptr ~Type() is called<br>}<br><br>int main() {<br>  std::unique_ptr&lt;Type&gt; ptr = std::make_unique&lt;Type&gt;();// create a new ptr<br>  // std::unique_ptr&lt;Type&gt; new_ptr = ptr; This is not allowed!<br>  std::unique_ptr&lt;Type&gt; new_ptr = std::move(ptr); <br>  // Move is allowed ptr is now null!<br>  callMeAFunction(new_ptr); // transfers ownership<br>  // new_ptr is now null<br>} </pre><p>b.)std::shared_ptr allows us to create a shared pointer to an object (duh, again?). This is useful when you want to have multiple pointers pointing to the same object while ensuring proper ownership and memory management. Unlike std::unique_ptr, which disallows copying, a std::shared_ptr can be copied. It uses reference counting to manage ownership of the object it points to.</p><p>When a std::shared_ptr is passed to a function, the reference count is incremented, indicating that there is another owner of the pointer. This ensures that the object will not be destroyed as long as there is at least one std::shared_ptr pointing to it. When the function goes out of scope, the reference count is decremented. If the reference count reaches zero, meaning no std::shared_ptr instances point to the object anymore, the destructor of the object is called, and the memory is freed.</p><p>This mechanism allows for safer and more flexible memory management in situations where multiple parts of a program need to share ownership of an object, without risking memory leaks or dangling pointers.</p><pre>void OwnerOne(std::shared_ptr&lt;Type&gt; ptr){<br>}<br><br>int main() { <br>  std::shared_ptr&lt;Type&gt; ptr = std::make_shared&lt;Type&gt;();<br>  // copy constructor is called increasing the reference count for the pointer<br>  std::shared_ptr&lt;Type&gt; new_ptr = ptr; // reference count is 2<br>  OwnerOne(new_ptr); // created a copy and reference count is now 3<br>  ptr.reset(); // Reference count becomes one <br>  new_ptr.reset(); // Reference count becomes zero, Type() object is deleted<br>}</pre><p>And that’s it! These are some cool features that you might encounter while working with C++. I hope you had a fun time reading this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11b1f8b07915" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>