Posts

Showing posts with the label Clean Code

3. Template type deduction when template parameter is neither a pointer nor a reference

Image
Motivation for writing, this post is, In the past I interviewed many candidates for C++ and this was one of the topics where I found candidates getting confused with templates type deduction when template parameter is neither a pointer nor a reference. What is a template? As a quick short introduction, In General template, is a ‘pattern’ that is used to create specific instances. Moving from generics to specifics. This means you can generate specific instances by repeating the same pattern again and again for different types of inputs provided. For example, below we have the same presentation template used for C++ and Python-specific presentations for different audiences. C++ Templates In C++ a  template is a class or a function that we parameterize with a set of types or values. We use templates to represent concepts that are best understood as something very general from which we can generate specific types and functions by specifying arguments, such as the element type double.

2. Why should someone Learn C++ ?

Image
Recently I surveyed Students/C++ Beginners and Programmers good to see above 80% are using C++11 or above C++11. Also, I am seeing that the popularity of C++ rising quickly. So it really becomes very important to understand Modern C++. Having said that, I get the question from many students why should I learn C++? So before why should I learn C++ Let's see what is C++? How C++ is different from other programming languages.  What is C++? "C++ is a language for defining and using light-weight abstractions". It has significant strengths in areas where hardware must be handled effectively and there is significant complexity to cope with. -- Bjarne Stroustrup a) C++ is a General-purpose language that is fit for all domains . b) Low-level things like C and Abstraction or High Level for Human representing things. c) High-level abstraction(Human Understanding) efficient enough close enough to the hardware(Machine Understanding) for demanding or resource-intensive tasks. d) C++ ha

1. nullptr C++

Image
Why nullptr ? what is nullptr ? But we already have NULL why nullptr ? Well the problem is how to distinguish between NULL and 0? we have #define NULL 0. The NULL pointer and an integer 0 cannot be distinguished well for overload resolution. For example, given two overloaded functions The call f(0) unambiguously resolves to f(int). There is no way to write a call to f(char*) with a null pointer value without writing an explicit cast (i.e., f((char*)0)) or using a named variable. Naming NULL Further, programmers have often requested that the null pointer constant have a name (rather than just 0). This is one reason why the macro NULL exists, although that macro is insufficient. (If the null pointer constant had a type-safe name, this would also solve the previous problem as it could be distinguished from the integer 0 for overload resolution and some error detection.) To avoid these problems, 0 must mean only one thing (an integer value), and we need to have a different name t