While performing a code review on a refactoring project, I stumbled upon a change that took a line of code from this state: Widget myWidget{42}; to that: auto myWidget = Widget{42}; Well, in the actual case the type wasn’t called Widget and the initialization value wasn’t exactly 42. But that’s the gist of it. What’s […]
Today’s guest post is written by guest author Miguel Raggi. Miguel is a Computer Science and Math professor at UNAM, Mexico’s largest university. He loves clean, expressive, performant C++ code (and strives to convince students to write it in this way!). Miguel is the author of discreture, an open source C++ library to efficiently generate […]
Passing inputs and getting outputs from a function is pretty straightforward and uncontroversial: inputs get in as function arguments by const reference (or by value for primitive types), outputs get out via the return type. Output function(Input1 const& input1, int input2); Now this is all well, until input-output values get in the picture. An input-output value is […]
Today’s guest post is written by Federico Kircheis, a (mainly C++) developer in Berlin, always looking how to improve himself, and finding interesting problems to solve. Federico talks to us about a little known compiler feature that could have an impact on how you design code: function poisoning. Also interested in writing on Fluent C++? […]
Do you wish you had a later version of C++ in your production code? If you do, you’re not alone: a lot of C++ developers today don’t work with a compiler that supports the latest version of the standard. It could be for many reasons: perhaps you have a lot of legacy code to migrate, […]
Travelling back from ACCU conference a couple of weeks ago, one of the insights that I’ve brought back with me is from Kevlin Henney’s talk Procedural Programming: It’s Back? It Never Went Away. It’s surprisingly simple but surprisingly insightful, and it has to do with early return statements. Early return statements are controversial in the programming […]
C++ supports default parameters, but with some constraints. We’ve seen that default arguments had to be positioned at the end of a function’s parameters, and also that default parameters are interdependent: indeed, to provide a non-default value to one of them, you have to also pass a value to those that come before it. We’ve […]
Over the posts of the series on default parameters, we’ve come across two constraints of default parameters in C++. The first one is that all the default parameters have to be at the end of the arguments list of a function. This can make an interface less natural, because arguments are no longer grouped in […]
There is a particular case for default parameters: it’s when their type is a template type. Even though the idea is similar to the regular default parameters, there are some subtleties that are worth mentioning. To illustrate this, I will use the example of map_aggregator, the output iterator for aggregating data into a map, for […]
“Should I use overloads or default parameters”, haven’t you asked yourself that question? When designing an interface where the user can leave the value of an argument up to the API, two approaches are possible: Using a default parameters: void drawPoint(int x, int y, Color color = Color::Black); And using overloading: void drawPoint(int x, int […]