## Code critique: Fear of templates makes power set implementation useless

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

## Tippet: Use reference_wrapper to create views of data

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

## How to write a standard-like algorithm

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

## The bell has tolled for rand()

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

## Math temporarily disabled on the blog

I have had to disable the MathJax-LaTeX plugin due to a parse error in the PHP in the latest version (v 1.3.3) that brings down the entire blog. That means that equations and any other math stuff will not display properly. The developer is already aware of the problem as of 3 days ago, and working on a fix, so this should just be a temporary hiccup.

## Overthinking: A critique of Herb Sutter’s recommendations on function arguments

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

## Tippet: Printing numeric values for chars (and (u)int8_t)

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

## How to read an entire file into memory in C++

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

## Tippet: No need to square, sum, and sqrt manually anymore

I hang out a lot with engineers and game programmers, so that means I tend to see a lot of code dealing with physics problems. One of the most commonly used bits of code in both domains has to be the Euclidean distance equation. However, few programmers realize that that equation is a lot easier than it used to be. Continue Reading