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)
Author: elbeno
How many bugs are left? The Lincoln Index
You’re working on some software, and you have some QA folks testing it. How do you know how many bugs are left? You know the bugs that the testers find, but how can you estimate the number that aren’t yet found? If your testers work independently, and if your feature is not under continuous development… Continue reading How many bugs are left? The Lincoln Index
Adopting C++11: no-brainer features
C++11/14 is a significant change from C++98/03, and features like move semantics take a while to get used to. Also, people tend to be quite conservative about adopting new features (especially if they look unfamiliar). It took us in the games industry a while to move to C++ from C. But here are what I… Continue reading Adopting C++11: no-brainer features
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 5)
Part 1 Part 2 Part 3 Part 4 Part 5 Postscript So far, we can print containers, but what about arrays? And what about “pretty-printing” strings – perhaps we need to wrap them with quotes. Well, we know that with the existing code, both arrays and strings count as outputtable. Both std::string and char* (and… Continue reading How to print anything in C++ (part 5)
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)
How to print anything in C++ (part 3)
Part 1 Part 2 Part 3 Part 4 Part 5 Postscript So far, we’ve dealt with things that are already outputtable with operator<<, and things that are iterable with begin() and end(). To round out the “containers”, we need to deal with pair and tuple. It’s simple to print a pair: template struct stringifier_select {… Continue reading How to print anything in C++ (part 3)