Articles & Books

Modern C++ Features – User-Defined Literals--Arne Mertz

Learn more about litterals.

Modern C++ Features – User-Defined Literals

by Arne Mertz

From the article:

User-defined literals are a convenient feature added in C++11.

C++ always had a number of built-in ways to write literals: Pieces of source code that have a specific type and value. They are part of the basic building blocks of the language:

32 043 0x34   //integer literals, type int
4.27 5E1      //floating point literals, type double
'f', '\n'     //character literals, type char
"foo"         //string literal, type const char[4]
true, false   //boolean literals, type bool

These are only the most common ones, there are many more, including some newcomers in the newer standards. Other literals are nullptr and different kinds of prefixes for character and string literals. There also are suffixes we can use to change the type of a built-in numeric literal:

32u     //unsigned int
043l    //long
0x34ull //unsigned long long
4.27f   //float
5E1l    //long double

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?