Where Are My Pointers? : Ditching C++98
When i did the IRC server, I had no choice but to use C++98. That’s just how things are at my university, i remember a friend telling me that C++11 was almost a different language, he was right.
Why I Upgraded to C++20
The most obvious change I made was getting rid of all the naked pointers. Back in the original version, everything was manually allocated and deallocated using new and delete, which not only made memory management more tedious but also increased the risk of leaks and dangling pointers.
Now, everything is managed using std::shared_ptr and std::unique_ptr, which ensures proper ownership and automatic cleanup.
Before :
clients_[fd] = new Client(fd);
After :
clients_[fd] = std::make_shared<Client> (fd);
Besides this, I also used enable_shared_from_this to handle shared ownership more safely when dealing with channels:
class Channel : public std::enable_shared_from_this<Channel>
Learning auto and Other Modern C++ Features
Another big thing I learned while doing this refactor was how to properly use auto. It might seem trivial, but coming from C++98, where everything had to be explicitly typed, it felt like magic.
Before:
std::vector<Client*>::iterator it = clients.begin();
After:
auto it = clients.begin();
Not only does it make the code look cleaner, but it also saves time and avoids unnecessary verbosity.
If I Had Started in C++20 From Scratch…
This project was originally designed with my C++98 brain, so even though I modernized it, there are still structural decisions that don’t fully take advantage of what C++20 offers. If I had started this project from scratch knowing modern C++, I probably would have structured it completely differently.
For example:
- The structure and handling of the channel pointers would be different
- I would have leveraged
std::optionalandstd::variantinstead of relying on error-prone null checks.
Refactoring this project was a fun learning experience, and while it still retains most of its C++98 roots, it’s now much more efficient, safer, and easier to maintain.
Check out the updated code here.