Blog Archive

Friday, May 22, 2020

Some tricks to speed up your C++ code writing speed


3 Simple C++17 Features That Will Make Your Code Simpler

Published June 19, 2018
This article is a guest post written by guest author jft.
C++17 has brought a lot of features to the C++ language. Let’s dig into three of them that help make coding easier, more concise, intuitive and correct.
We’ll begin with Structured Bindings. These were introduced as a means to allow a single definition to define multiple variables with different types. Structured bindings apply to many situations, and we’ll see several cases where they can make code more concise and simpler.
Then we’ll see Template Argument Deduction, which allows us to remove template arguments that we’re used to typing, but that we really shouldn’t need to.
And we’ll finish with Selection Initialization, which gives us more control about object scoping and lets us define values where they belong.
So let’s start with structured bindings.

Structured Bindings

Structured Bindings allow us to define several objects in one go, in a more natural way than in the previous versions of C++.

From C++11 to C++17

This concept is not new in itself. Previously, it was always possible to return multiple values from a function and access them using std::tie.
Consider the function:
This returns three variables all of different types. To access these from a calling function prior to C++17, we would need something like:
Where the variables have to be defined before use and the types known in advance.
But using Structured Bindings, we can simply do this as:
which is a much nicer syntax and is also consistent with modern C++ style using auto almost whenever possible.
So what can be used with a Structured Binding initialization? Basically anything that is a compound type – structpair and tuple. Let’s see several cases where it can be useful.

Returning compound objects

This is the easy way to assign the individual parts of a compound type (such as a struct, pair etc) to different variables all in one go – and have the correct types automatically assigned. So let’s have a look at an example. If we insert into a map, then the result is a std::pair:
And if anyone is wondering why the types are not explicitly stated for pair, then the answer is Template Argument Deduction in C++17 – keep reading!
So to determine if the insert was successful or not, we could extract the info from what the insert method returned:
The problem with this code is that a reader needs to look up what .second is supposed to mean, if only mentally. But using Structured Bindings, this becomes:
Where itelem is the iterator to the element and success is of type bool, with true for insertion success. The types of the variables are automatically deduced from the assignment – which is much more meaningful when reading code.
As a sneak peek into the last section, as C++17 now has Selection Initialization, then we could (and probably would) write this as:
But more on this in a moment.

Iterating over a compound collection

Structured Bindings also work with range-for as well. So considering the previous mymap definition, prior to C++17 we would iterate it with code looking like this:
Or maybe, to be more explicit:
But Structured Bindings allow us to write it more directly:
The usage of the variables key and value are more instructive than entry.first and entry.second – and without requiring the extra variable definitions.

Direct initialization

But as Structured Bindings can initialize from a tuple, pair etc, can we do direct initialization this way?
Yes we can. Consider:
which defines variables a as type char with initial value ‘a’, i as type int with initial value 123 and b as type bool with initial value true.
Using Structured Bindings, this can be written as:
This will define the variables aib the same as if the separate defines above had been used.
Is this really an improvement over the previous definition? OK, we’ve done in one line what would have taken three but why would we want to do this?
Consider the following code:
Both iss and name are only used within the for block, yet iss has to be declared outside of the for statement and within its own block so that the scope is limited to that required.
This is weird, because iss belongs to the for loop.
Initialization of multiple variables of the same type has always been possible. For example:
But what we’d like to write – but can’t – is:
With Structured Bindings we can write:
and
Which allows the variables iss and name (and i and ch) to be defined within the scope of the for statement as needed and also their type to be automatically determined.
And likewise with the if and switch statements, which now take optional Selection Initialization in C++17 (see below). For example:
Note that we can’t do everything with structured bindings, and trying to fit them in to every situation can make the code more convoluted. Consider the following example:
Here variable box is defined as type unsigned long and has an initial value returned from stoul(p)stoul(), for those not familiar with it, is a <string> function which takes a type std::string as its first argument (there are other optional ones – including base) and parses its content as an integral number of the specified base (defaults to 10), which is returned as an unsigned long value.
The type of variable bit is that of an iterator for boxes and has an initial value of .begin() – which is just to determine its type for auto. The actual value of variable bit is set in the condition test part of the if statement. This highlights a constraint with using Structured Bindings in this way. What we really want to write is:
But we can’t because a variable declared within an auto type specifier cannot appear within its own initializer! Which is kind of understandable.
So to sum up, the advantages of using Structured Bindings are:
  • a single declaration that declares one or more local variables
  • that can have different types
  • whose types are always deduced using a single auto
  • assigned from a composite type.
The drawback, of course, is that an intermediary (eg std::pair) is used. This needn’t necessarily impact upon performance (it is only done once at the start of the loop anyhow) as move semantics would be used where possible – but note that where a type used is non-moveable (eg like std::array) then this could incur a performance ‘hit’ depending upon what the copy operation involved.
But don’t pre-judge the compiler and pre-optimize code! If the performance isn’t as required, then use a profiler to find the bottleneck(s) – otherwise you are wasting development time. Just write the simplest / cleanest code that you can.

Template Argument Deduction

Put simply, Template Argument Deduction is the ability of templated classes to determine the type of the passed arguments for constructors without explicitly stating the type.
Before C++17, to construct an instance of a templated class we had to explicitly state the types of the argument (or use one of the make_xyz support functions).
Consider:
Here, p is an instance of the class pair and is initialized with values of 2 and 4.5. Or the other method of achieving this would be:
Both methods have their drawbacks. Creating “make functions” like std::make_pair is confusing, artificial and inconsistent with how non-template classes are constructed. std::make_pairstd::make_tuple etc are available in the standard library, but for user-defined types it is worse: you have to write your own make_… functions. Doh!
Specifying template arguments, as in:
should be unnecessary since they can be inferred from the type of the arguments – as is usual with template functions.
In C++17, this requirement for specifying the types for a templated class constructor has been abolished. This means that we can now write:
or
which is the logical way you would expect to be able to define p!
So considering the earlier function mytuple(). Using Template Argument Deduction (and auto for function return type), consider:
This is a much cleaner way of coding – and in this case we could even wrap it as:
There is more to it than that, and to dig deeper into that feature you can check out Simon Brand’s presentation about Template Argument Deduction.

Selection Initialization

Selection Initialization allows for optional variable initialization within if and switch statements – similar to that used within for statements. Consider:
Here the scope of a is limited to the for statement. But consider:
Here variable a is used only within the if statement but has to be defined outside within its own block if we want to limit its scope. But in C++17 this can be written as:
Which follows the same initialization syntax as the for statement – with the initialization part separated from the selection part by a semicolon (;). This same initialization syntax can similarly be used with the switch statement. Consider:
Which all nicely helps C++ to be more concise, intuitive and correct! How many of us have written code such as:
Where a before the second if hasn’t been initialized properly before the test (an error) but isn’t picked up by the compiler because of the earlier definition – which is still in scope as it isn’t defined within its own block. If this had been coded in C++17 as:
Then this would have been picked up by the compiler and reported as an error. A compiler error costs much less to fix than an unknown run-time problem!

C++17 helps making code simpler

In summary, we’ve seen how Structured Bindings allow for a single declaration that declares one or more local variables that can have different types, and whose types are always deduced using a single auto. They can be assigned from a composite type.
Template Argument Deduction allows us to avoid writing redundant template parameters and helper functions to deduce them. And Selection Initialization make the initialization in if and switch statements consistent with the one in for statements – and avoids the pitfall of variable scoping being too large.

References


Reference:
https://www.fluentcpp.com/2018/06/19/3-simple-c17-features-that-will-make-your-code-simpler/

No comments:

Post a Comment