Dealing with smart pointers is a daily task for any C++ programmer and how to pass them around can get tricky when you are looking to achieve optimal performance. Here are some tips and general guidelines to follow for passing shared_ptr and unique_ptr as function arguments and at the time of return.

As always, prefer unique_ptr to shared_ptr, unless you intend to share ownership.

Introduction

Always ask yourself “Do I really need to pass a smart pointer ?”

You should always assess…


Did you ever think who understands google.com when you type that in your address bar? 🤔

How does this magic happen? ✨ There isn’t a delivery boy to hand over an HTML page directly. Let’s understand the crux of this superfast procedure 🚀 starting from typing a URL address to getting a page in your browser. 💻

If you ever need to set up a blog/website, this is a must-know article that contains all the understanding of DNS, the lookup process, types of DNS records i.e. all you need to know explained within a single article.

DNS: Internet’s own complex library

Table of Contents

Here’s the outline of…


A lambda function is a function that you can write inline in your source code In this article, I’ll first explain why lambdas are great, what are closures vs how lambdas are different from closures? — with some examples — and then I’ll walk through all of the details of what you can do with lambda.

What are closures vs how lambdas are different from closures?

They look similar!!

Table of Contents

Just your everyday functions

When most people think of functions, they think of named functions such as -


How virtual vs. multiple inheritance affect class object construction order?

I’ll try to keep this post short and simple 😊

Table of Contents

Example: A class having virtual and/or multiple inheritance

class Xis declared here as inherited by D1, D2 which in turn are inherited by other class in a complex manner by other classes.


Using In-member initialization, using constructors smartly and using class members functions in a safe and proper way to avoid mistakes

Clean code!

Table of Contents

1. Use member initializers in the same order as their declaration

Member variables are always initialized in the order they are declared in the class definition.

The order in which you write them in the constructor initialization list is ignored 🥴

Make sure the constructor code doesn’t confusingly specify different orders. For…


Some puzzles from various talks, blog posts, and other bits

Table of Contents

Puzzle 1: Capturing unique_ptr by reference

unique_ptr<A> myFun()
{
unique_ptr<A> pa(new A());
return pa;
}

const A& rA = *myFun();

This code compiles but rA contains garbage. Why is this code invalid?

Note: if we assign the return of myFun to a named unique_ptr variable before dereferencing it, it works fine.

Answer:

The unique_ptr will pass the ownership to another unique_ptr, but in this code, there is nothing to…


Let’s discuss what coroutines are in general and how C++20 is introducing them

Table of Contents

1. Prerequisite Terminology
2. What are coroutines?
3. Coroutines vs Subroutines?
4. Coroutines vs Threads
5. Applications of coroutines
6. Example in Python
7. How to simulate coroutines in traditional C++
8. Coroutines in C++20
9. Restrictions

Prerequisite Terminology


How to use a custom deleter with an unique_ptr and shared_ptr

Related post: https://medium.com/pranayaggarwal25/a-tale-of-two-allocations-f61aa0bf71fc

Table of Contents

Introduction

Why and when would we need something like that?

Case 1: In order to fully delete an object sometimes, we need to do some additional action. …


Also known as Forwarding reference

Prerequisites: Rvalue references

Table of Contents:

1. Introduction
2. The double life of “&&”
3. How to identify a Universal Reference
4. Universal Reference in templates
5. Universal Reference in auto
6. How does the magic of Univeral Reference work?
7. Handle with care
8. What is the need? The perfect forwarding problem
9. How to work with Universal References?
10. Universal Reference in “typedef”
11. Universal Reference in “decltype”
12. The ultimate Truth

Introduction

Every rvalue ref is denoted by ‘&&’ but vice versa is not true.

Rvalue reference ⇒ “&&
&& Rvalue reference.

&&” in source…


rvalue references

Contents

Introduction

In quest of performant code, C++11 introduced move semantics and to achieve that, a new terminology & tool — rvalue references.

If you find yourself always confused about lvalues, rvalues, and their references, this is the right article is for you. If you know them well, I hope you’ll be kind enough to let me know your feedback and suggestions.

Backstory

Prior to C++11…

Pranay Kumar

Computer Scientist @ Adobe Systems https://pranayaggarwal.github.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store