For reasons that I cannot fathom, templates terrify many C++ programmers. Not just beginners; hang around any sizable C++ community for any length of time and you will inevitably see someone who is otherwise quite competent going to irrational lengths to avoid writing a template. This fear baffles me, because templates are not really any harder to write than non-templates – in fact, in many cases you can simply replace the hard-coded types with T, add the template declaration, and you’re done. This refusal to simply go generic when it is obviously the way to go can make an otherwise potentially useful bit of code almost entirely useless. Continue Reading

A while ago a guy I know was working on a program that involved a large number of “jobs” read from a file/database. The issue he had was that he wanted different “views” of those jobs – for example, he wanted a view of the jobs sorted by job ID, another of the jobs sorted by start/end time, another sorted by priority, and so on. Continue Reading

One of my pet peeves about most C++ courses, tutorials, and books is that they seem to pointedly ignore algorithms. I don’t mean algorithms from a theoretical perspective, I mean the standard algorithms library – I am stunned at how many C++ teachers insist their students don’t use algorithms in their assignments. And aside from the actual standard algorithms themselves, most books, tutorials, and courses make absolutely no effort to discuss algorithms that follow the same general design. In my opinion, writing a standard-like algorithm should be one of the key parts of a modern C++ beginner’s course outline. Continue Reading

In their recent meeting in Urbana, the C++ standard committee took the rare step of removing several outdated facilities from (what will probably become) C++17. Most of the things removed had been deprecated since C++11, but there was one surprising item on the list: std::random_shuffle(). Its removal is a signal of a big change that has been building in the background for a while: the end of std::rand(). Continue Reading

When move semantics were introduced in C++11, it sparked a lot of discussion about how to pass arguments to functions that will take a copy of the argument value (for example, the “set” half of get/set functions). That confusion seemed to subside when the “by value by default” suggestion started to make the rounds, but in his CppCon2014 talk, Herb Sutter set off a whole new round of debate. Sutter makes two arguments for why “by const-ref by default” is the best rule. I don’t buy them. Continue Reading

When you’re working with bytes as numeric values – rather than characters – there’s a gotcha that can burn you when you try to stream those values out using standard library streams. The insertion operator for all character types – char, signed char, and unsigned char – prints the character value, not the numeric value. Lots of beginners struggle with this and even many experts don’t know the easy fix. Continue Reading

Over ten years ago I first wrote an article about reading an entire file into memory in C++. It has become my most copied work (always without attribution), and every time I’ve set up a new site or blog, I’ve included an updated version – which again usually ranks among the most popular pages or posts there. So, it’s time again for the 2014 version of How to read an entire file into memory in C++. Continue Reading