A persistent myth about STL’s remove (and friends)

There seems to be a persistent myth about STL’s remove, remove_if, etc. Ask even a relatively experienced C++ programmer to explain this code. vector v = { 1,2,3,4,5 }; v.erase(remove_if(v.begin(), v.end(), [] (int i) { return (i & 1) == 0; }), v.end()); They’ll recognize the erase-remove idiom and correctly say that it’s removing even… Continue reading A persistent myth about STL’s remove (and friends)

VS2010 woes: tuples as map keys

Another day, another compiler/library bug. If you’re unfortunate enough to still be using Visual Studio 2010, don’t use tuples as map keys. #include #include #include using namespace std; typedef tuple FooT; typedef map MapT; int main(int, char*[]) { MapT m; // put a value in the map { FooT f(nullptr, 0); m[f] = 1337; }… Continue reading VS2010 woes: tuples as map keys

A problem with C++ lambdas?

C++ lambdas are wonderful for all sorts of reasons (especially with their C++14-and-beyond power). But I’ve run into a problem that I can’t think of a good way around yet. If you’re up to date with C++, of course you know that rvalue references and move semantics are a major thing. At this point, there… Continue reading A problem with C++ lambdas?

Clang/GCC weirdness

Consider the following code: #include #include using namespace std; // base template template struct what_type { void operator()() { cout

How to print anything in C++ (postscript)

Part 1 Part 2 Part 3 Part 4 Part 5 Postscript Refactoring! My initial plan for customizing opener/closer/separator for containers turned out to be unwieldy: I realized that it wouldn’t be possible for me to provide default specializations and also allow clients to specialize. Also, you may have noticed that the code for printing pairs,… Continue reading How to print anything in C++ (postscript)

How to print anything in C++ (part 4)

Part 1 Part 2 Part 3 Part 4 Part 5 Postscript Callable things. There are several types: functions member functions std::functions bind expressions lambdas objects that support operator() (function objects) So, going back to my tag code, so far (with everything I’ve added) and including callable things, it will look like: template using stringifier_tag =… Continue reading How to print anything in C++ (part 4)