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.
You should always assess…
Problem: We are given a number N and we have to find the smallest possible palindrome greater than N that can be formed by using the same set of digits as in N. This problem is asked in many interviews and require an understanding step-by-step approach, also have applications in other manipulation problems.
What is a palindrome? A word, phrase, or sequence that reads the same backward as forwards, e.g. madam or 12321.
Let’s say we’re given the number 4697557964 which…
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.
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?
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 😊
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
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
unique_ptr<A> pa(new A());
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.
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
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
How to use a custom deleter with an
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
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
Every rvalue ref is denoted by ‘&&’ but vice versa is not true.
Rvalue reference ⇒ “
&&” ⇏ Rvalue reference.
&&” in source…