Articles & Books

void foo(T& out) - How to fix output parameters--Jonathan Müller

Or how to improve readability and reduce errors.

void foo(T& out) - How to fix output parameters

by Jonathan Müller

From the article:

There are some cases where you need to return a value from a function but cannot use the return value. It happens, for example, in functions where you want to return multiple values at once. While you can pass multiple inputs to a function - the parameters, you cannot pass multiple return values in the same way.

C++ programmers tend to use a good old (lvalue) reference for that. You take a non-const reference as parameter and assign the output to that reference. The caller will pass a variable and upon function completion find the value of the variable changed.

Yet this approach has some problems: For starters, it is not obvious when just looking at the call that the variable is going to be changed. This is the reason that C++ style guides such as the one used by Google recommend using a pointer for that. The caller then has to explicitly pass in the address of the variable, making it explicit.

But with a pointer you can now pass in nullptr, you have to check for that in the function: A pointer where you really mean “reference” does not follow the guidelines I’ve been advocating for.

So is there not a universal solution?

There is, but first we need to understand the full scope of the problem.

Quick Q: Is std::vector so much slower than plain arrays?

Quick A: A vector isn’t slower than an array when they do the same things. But it lets you do much more…

Some time ago on SO:

Is std::vector so much slower than plain arrays?

Using the following:

g++ -O3 Time.cpp -I <MyBoost>
./a.out
UseArray completed in 2.196 seconds
UseVector completed in 4.412 seconds
UseVectorPushBack completed in 8.017 seconds
The whole thing completed in 14.626 seconds


So array is twice as quick as vector.

But after looking at the code in more detail this is expected; as you run across the vector twice and the array only once. Note: when you resize() the vector you are not only allocating the memory but also running through the vector and calling the constructor on each member.

Re-Arranging the code slightly so that the vector only initializes each object once:

std::vector<Pixel>  pixels(dimensions * dimensions, Pixel(255,0,0));

Now doing the same timing again:

g++ -O3 Time.cpp -I <MyBoost>
./a.out
UseVector completed in 2.216 seconds


The vector now performance only slightly worse than the array. IMO this difference is insignificant and could be caused by a whole bunch of things not associated with the test.

I would also take into account that you are not correctly initializing/Destroying the Pixel object in the UseArrray() method as neither constructor/destructor is not called (this may not be an issue for this simple class but anything slightly more complex (ie with pointers or members with pointers) will cause problems.

Presenting Code

How should we present code? - is the question.

Presenting Code

by Jens Weller

From the article:

At CppCon 2015 I decided to give a small lightning talk on how to present code in the coming year. This was a reflection on visiting many C++ related conferences and seeing many talks live and online...

Visiting variants using lambdas (2: recursive variants) -- Vittorio Romeo

The article addresses the problem of lambda-based `std::variant` and `boost::variant` recursive visitation. Starting from the definition of a "recursive variant", it shows the concept and implementation behind a `make_recursive_visitor` variadic function that can be used to create "local" visitors by using lambdas.

visiting variants using lambdas - part 2

By Vittorio Romeo

From the article:

A "recursive" variant is a variant which can contain itself, and can be used to represent recursive structures. [...] Bringing algebraic data types from the functional programming world into C++ isn't enough - we're also going to adopt another powerful construct: the Y Combinator. [...] Now we can put everything together to finally visit a recursive variant using lambdas!

Tutorial: Emulating strong/opaque typedefs in C++--Jonathan Müller

Who would like this feature?

Tutorial: Emulating strong/opaque typedefs in C++

by Jonathan Müller

From the article:

Last week, I’ve released my type_safe library. I described it’s features in the corresponding blog post but because the blog post got rather long, I couldn’t cover one feature: strong typedefs.

Strong or opaque typedefs are a very powerful feature if you want to prevent errors with the type system - and as I’ve been advocating for, you want that. Unlike “normal” typedefs, they are a true type definition: they create a new type and allow stuff like overloading on them and/or prevent implicit conversions.

Visiting variants using lambdas (1) - - Vittorio Romeo

The article covers a technique that allows variant types (both `std::variant` and `boost::variant`) to be visited using lambdas, without having to explicitly create a functor.

visiting variants using lambdas - part 1

By Vittorio Romeo

From the article:

Visiting a variant is usually done by writing a visitor struct/class outside of the scope where the variant is actually being visited. [...] It is possible to build such an object locally in the call site [...] by implementing something similar to `std::overload`.

The “unsigned” Conundrum--Tony “Bulldozer00” DaSilva

Are you clear with unsigned?

The “unsigned” Conundrum

by Tony “Bulldozer00” DaSilva

From the article:

A few weeks ago, CppCon16 conference organizer Jon Kalb gave a great little lightning talk titled “unsigned: A Guideline For Better Code“. Right up front, he asked the audience what they thought this code would print out to the standard console:

Even though -1 is obviously less 1, the program prints out “a is not less than b“. WTF?

Overload 135 is now available

ACCU’s Overload journal of October 2016 is out. It contains the following C++ related articles.

Overload 135 is now available

From the journal:

Determinism: Requirements vs Features
A program can easily be non-deterministic. Sergey Ignatchenko considers how to define determinism. by Sergey Ignatchenko

Eight Rooty Pieces
Finding a square root is a common interview question. Patrick Martin demonstrates eight different ways to find a root. by Patrick Martin

Polymorphic Comparisons
Polymorphic comparisons require much boilerplate. Robert Mill and Jonathan Coe introduce a template utility for such comparisons. by Robert Mill and Jonathan Coe

C++ Synchronous Continuation Passing Style
Direct and continuation passing styles differ. Nick Weatherhead explains a continuation passing style for synchronous data flow. by Nick Weatherhead

Attacking Licensing Problems with C++
Software licenses are often crackable. Deák Ferenc presents a technique for tackling this problem. by Deák Ferenc