Modern C++ Programming — Things that shouldn’t be used anymore

Daksh Gupta
6 min readFeb 29, 2024

--

The Future of C++ programming Language

C++ was and is a great programming language and whether we realize this or not, many of our day to day databases, scripting engines and games are predominantly written using C++ and the language is here to stay in the near future.

The ecosystem of C++

Which means we’ll have to write new code in C++ in the near future, but we have a choice i.e. Legacy Vs. Modern C++

Modern C++ has evolved a lot with the time and we can write code without using many constructs of the legacy C++ which are considered problematic and one of the primary reasons to why newer systems programming languages like Golang and Rust came into existence.

The modern C++ avatar came into existence with C++11 (i.e. 2011) and it’s releasing the updates regularly every 3 years since then. The current version of C++ is C++23.

So here are the some of the things which we should NOT be use while writing new modern C++ code.

This is not an exhaustive list, but contains some of the obvious ones which we must avoid unless otherwise absolutely needed

C++ is Backward Compatible and this capability comes with a price

C++ compiler is backward compatible which means new version of compilers can compile the old legacy constructs / code of C++ also, which means there is no way to prevent a software developer from using the constructs of pre C++11.

Why C++11? Because it was the first major step taken towards making C++ a modern programming language.

Maintaining backward compatibility is NOT a technical constraint, it's more about the continuity of business and support.

This in my opinion is the biggest problem in the evolution as well as in the acceptability of C++ as a modern programming language.

Backwards compatibility will never let C++ get rid of the baggages deemed unnecessary and problematic, even though we can live without it.

Legacy C++ Constructs

This also prevents new programmers from choosing C++ and one cannot learn and use Modern C++ without knowing and understanding the legacy capabilities and associated constraints.

However, as a C++ programmer, we can make sure we don’t use the old baggages and treat them as nonexistent.

Memory Allocation & De-allocation

Never use new, delete, malloc, free etc to allocate and deallocate the memory

Use smart pointers instead, like unique_ptr<> , shared_ptr<> because it takes care of allocation and deallocation automatically thereby preventing unnecessary memory usage and more importantly memory leaks.

Virtual Inheritance

The most unnecessary thing ever existed in C++ (in my personal opinion). Everything, I repeat Everything, we want to do can be done without using the complications of Virtual Inheritance

Virtual inheritance is used for handling what we call it as diamond inheritance dependency as show in the picture below

Virtual Inheritance — Diamond Dependency

Virtual inheritance is a big NO NO, there isn’t exist a problem which can’t be solved using normal constructs of the language or by using normal inheritance.

In my opinion, this virtual inheritance may be created as an afterthought where programmers are creating unchecked inheritances in the name of reusability (which again is not needed actually, the composition works as well in many scenarios).

Operator Overloading

C++ Objects are NOT mathematical constructs, nor it was supposed to be.

In C++, operator overloading is used for applying mathematical operations on the objects. Why on earth we need to do do +, -, *, ||, && on the objects?

There are built in types to do that, and if we have to use something like this then we’re creating the objects in the wrong way

Sometime there might be a valid reason to compare the objects so that they can be used in algorithms like sorting etc but the better way to do so is by creating a comparator function to compare the specific data of the objects.

Please consider the fact that operator overloading is not popular and many cases not allowed in other object oriented programming languages.

It’s just increases the complexity of implementation for a simple problem.

For creating comparators, we can also use STL in many cases as below.

  std::less<int> lessThan;
std::greater<int> greaterThan;

// <int> can be replaced with <user-defined_type>
// by creating a custom comparator comparator function

Raw Arrays & Pointers

Arrays & Pointers in C++

You should never create a list or arrays as below

int array[5] = {1,2,3,4,5}
int dynamic_array = new int[5]

Instead, you MUST use the STL arrays for static arrays and vectors, deque, list & forward_list for dynamic arrays

// Static Array
std::array<int, 5> stl_array = {1, 2, 3, 4, 5};

// Dynamic Array
vector<int> dv;
deque<int> dd;
list<int> dl;
forward_list<int> df;

Now pointers can be used to access the memory location so that we can access the value at the memory location

int value = 10;
int *ptr = &value;
*ptr = 100; // now value = 100

In reality, if you have the variable as value you don’t need to use pointers to access or change the value of the variable. You can work directly on the variable and it will make your code clean.

Always remember, if you need to use pointers, then there is a high degree of chance that you can improve your code. Think about using the references or any other mechanisms instead.

Another benefit of using other mechanisms like references is that you need to think less about copy constructors.

Macros

Stop giving direction to compilers using Macros. Instead, use Meta Programming Constructs of Modern C++.

Organize your code in a better way and use things like const, constexpr, enum and STL to give specific direction to the compilers

I’ll stop here on what NOT to use. There are many more items on the list, but I hope you got a gist of it.

I’ll end this blog with what we should and must use to take the ultimate benefits of C++ as a modern programming language and that thing is STL i.e. the standard template library.

STL (Standard Template Library)

You need only STL i.e. the standard template library to create your software.

STL can be considered as the synonym of Modern C++ and one doesn’t need to use classes, inheritance, memory management to implement the required functionality.

Please consider STL on its own as a separate programming language. It will help you in writing Modern C++ code

I do hope that this blog was helpful

Thanks for Reading …!!!

Daksh

I’m a Software Consultant and creating softwares for 23 odd years now. I believe in the power of sharing knowledge and its impact in equipping software creators.

You may please also follow me on platforms like X & YouTube (link in Profile)

--

--

Daksh Gupta

🔹Software Product Development Consultant, Trainer, & Coach🔹 Tech Speaker & Content Creator 🔹 youtube.com/@Cognitive-Programmer 🔹 linkedin.com/in/iDaksh