C++14: What’s Important

Return type deduction

The capabilities auto are expanded in this release. C++ itself continues to be typesafe, but the mechanics of type safety are increasingly being performed by the compiler instead of the programmer.

In C++11, programmers starting using auto for declarations. This was keenly appreciated for things like iterator creation, when the fully qualified type name might be horrendous. Newly minted C++ code was much easier to read:

for (
auto ii = collection.begin() ; ...

In C++14, the use of auto is expanded in a couple of ways. One that makes perfect sense is that of return type deduction. If I write a line of code like this inside a function:

return 1.4;

It is obvious to both me and the compiler that the function is returning a double. So in C++14, I can define the function return type as auto instead of double:

auto
getvalue() {

The details of this new feature are pretty easy to understand. For example, if a function has multiple return paths, they need to have the same type. Code like this:

auto
f(
int i)

{

  if (
i < 0 )

    return -1;

  else

    return 2.0

}

might seem like it should obviously have a deduced return type of double, but the standard prohibits this ambiguity, and the compiler property complains:

error_01.cpp:6:5: error: 'auto' in return type deduced as 'double' here but deduced as 'int' in
      earlier return statement
    return 2.0
    ^
1 error generated.

There several good reasons why deducing the return type is a plus for your C++ programs. First, there are times when you need to return a fairly complex type, such as an iterator, perhaps when searching into a standard library container. The auto return type makes the function easier to write properly, and easier to read. A second (maybe less obvious) reason is that using an auto
return type enhances your ability to refactor. As an example, consider this program:

#include
<iostream>

#include
<vector>

#include
<string>

struct record
{

   std::string
name;

   int id;

};

auto
find_id(
const std::vector<record>
&people,

             const std::string
&name)

{

  auto
match_name = [&name](
const record&
r) ->
bool {

    return r.name
== name;

  };

  auto
ii = find_if(people.begin(), people.end(), match_name );

  if (ii
== people.end())

    return -1;

  else

    return ii->id;

}

int main()

{

  std::vector<record>
roster = { {
"mark",1},

                                 {"bill",2},

                                 {"ted",3}};

  std::cout
<< find_id(roster,
"bill")
<<
"\n";

  std::cout
<< find_id(roster,
"ron")
<<
"\n";

}

In this example, I’m not saving many brain cells by having find_id() return auto instead of int. But consider what happens if I decide that I want to refactor my record structure. Instead of using an integral type to identify the person in the record object, maybe I have a new GUID type:

struct record
{

   std::string
name;

   GUID
id;

};

Making that change to the record object will cause a series of cascading changes in things like the return types of functions. But if my function uses automatic return type deduction, the compiler will silently make the change for me.

Generic Lambdas

Another place where auto has insinuated itself is in the definitions of lambda parameters. Defining lambda parameters with an auto type declaration is the loose equivalent of creating a template function. The lambda will be instantiated in a specific embodiment based on the deduced types of the arguments.

This is convenient for creating lambdas that can be reused in different contexts. In the simple example below, I’ve created a lambda used as a predicate in a standard library function. In the C++11 world, I would have needed to explicitly instantiate one lambda for adding integers, and a second for adding strings.

With the addition of generic lambdas, I can define a single lambda with generic parameters. Although the syntax doesn’t include the keyword template, this is still clearly a further extension of C++ generic programming:

#include
<iostream>

#include
<vector>

#include
<string>

#include
<numeric>

int main()

{

  std::vector<int>
ivec = { 1, 2, 3, 4};

  std::vector<std::string>
svec = {
"red",

                                    "green",

                                    "blue" };

  auto
adder  = [](auto op1, auto op2){
return op1
+ op2; };

  std::cout
<<
"int
result : "

            <<
std::accumulate(ivec.begin(),

                               ivec.end(),

                               0,

                               adder
)

            <<
"\n";

  std::cout
<<
"string
result : "

            <<
std::accumulate(svec.begin(),

                               svec.end(),

                               std::string(""),

                               adder
)

            <<
"\n";

  return 0;

}

Which produces the following output:

int result
: 10

string
result : redgreenblue

Even if you are instantiating anonymous inline lambdas, employing generic parameters is still useful for the reasons I discussed earlier in this article. When your data structures change, or functions in your APIs get signature modifications, generic lambdas will adjust with recompilation instead of requiring rewrites:

std::cout
<<
"string
result : "

          <<
std::accumulate(svec.begin(),

                             svec.end(),

                             std::string(""),

                             [](auto
op1,auto op2){
return op1+op2;
} )

          <<
"\n";

Initialized Lambda Captures

In C++11, we had to start adjusting to the notion of a lambda capture specification. That declaration guides the compiler during the creation of the closure: an instance of the function defined by the lambda, along with bindings to variables defined outside the lambda’s scope.

New, useful features that make the language safer and more convenient.

In the earlier example on deduced return types, I had a lambda definition that captured a single variable, name, used as the source of a search string in a predicate:

auto
match_name = [&name](
const record&
r) ->
bool {

  return r.name
== name;

};

auto
ii = find_if(people.begin(), people.end(), match_name );

This particular capture gives the lambda access to the variable by reference. Captures can also be performed by value, and in both cases, the use of the variable behaves in a way that fits with C++ intuition. Capture by value means the lambda operates on a local copy of a variable; capture by reference means the lambda operates on the actual instance of the variable from the outer scope.

All this is fine, but it comes with some limitations. I think the one that the committee felt it needed to address was the inability to initialize captured variables using move-only semantics.

What does this mean? If we expect that lambda is going to be a sink for a parameter, we would like to capture the outer variable using move semantics. As an example, consider how you would get a lambda to sink a unique_ptr, which is a move-only object. A first attempt to capture by value fails:

std::unique_ptr<int>
p(
new int);

*p
= 11;

auto
y = [p]() { std::cout <<
"inside:
"
<<
*p <<
"\n";};

This code generates a compiler error because unique_ptr does not generate a copy constructor — it specifically wants to ban making copies.

Changing this so that p is captured by reference compiles fine, but it doesn’t have the desired effect of sinking the value by moving the value into the local copy. Eventually, you could accomplish this by creating a local variable and calling std::move() on your captured reference, but this is a bit inefficient.

The fix for this is a modification of the capture clause syntax. Now, instead of just declaring a capture variable, you can do an initialization. The simple case that is used as an example in the standard looks like this:

auto
y = [&r = x, x = x+1]()->
int {...}

This captures a copy of x and increments the value simultaneously. The example is easy to understand, but I’m not sure it captures the value of this new syntax for sinking move-only variables. A use case that takes advantage of this shown here:

#include
<memory>

#include
<iostream>

int main()

{

  std::unique_ptr<int>
p(
new int);

  int x
= 5;

  *p
= 11;

  auto
y = [p=std::move(p)]() { std::cout <<
"inside:
"
<<
*p <<
"\n";};

  y();

  std::cout
<<
"outside:
"
<<
*p <<
"\n";

  return 0;

}

In this case, the captured value p is initialized using move semantics, effectively sinking the pointer without the need to declare a local variable:

inside: 11
Segmentation fault (core dumped)

That annoying result is what you expect — the code attempts to dereference p after it was captured and moved into the lambda.

The [[deprecated]] Attribute

The first time I saw the use of the deprecated attribute in Java, I admit to a bit of language envy. Code rot is a huge problem for most programmers. (Ever been praised for deleting code? Me neither.) This new attribute provides a systematic way to attack it.

Its use is convenient and simple — just place the [[deprecated]] tag in front of a declaration — which can be a class, variable, function, or a few other things. The result looks like this:

class

[[deprecated]]
flaky {

};

When your program uses a deprecated entity, the compiler’s reaction is left up to the implementer. Clearly, most people are going to want to see some sort of warning, and also to be able to turn that warning off at will. As an example, clang 3.4 gave this warning when instantiating a deprecated class:

dep.cpp:14:3:
warning:
'flaky' is
deprecated [-Wdeprecated-declarations]

  flaky
f;

  ^

dep.cpp:3:1:
note:
'flaky' declared
here

flaky
{

^

Note that the syntax of C++ attribute-tokens might seem a bit unfamiliar. The list of attributes, including [[deprecated]], comes after keywords like class or enum, and before the entity name.

This tag has an alternate form that includes a message parameter. Again, it is up to the implementer to decide what to do with this message. clang 3.4 apparently ignores the message. The output from this fragment:

class

[[deprecated]]
flaky {

};

 [[deprecated("Consider
using something other than cranky"
)]]

int cranky()

{

   return 0;

}

 int main()

{

  flaky
f;

  return cranky();

}

does not contain the error message:

dep.cpp:14:10: warning: 'cranky' is deprecated [-Wdeprecated-declarations]
  return cranky();
         ^
dep.cpp:6:5: note: 'cranky' declared here
int cranky()
    ^

Binary Literals and Digit Separators

These two new features aren’t earth-shaking, but they do represent nice syntactic improvements. Small changes like these give us some incremental improvements in the language that improve readability, and hence, reduce bug counts.

C++ programmers can now create binary literals, adding to the existing canon of decimal, hex, and the rarely used octal radices. Binary literals start with the prefix 0b and are followed by binary digits.

In the U.S. and UK, we are accustomed to using commas as digit separators in written numbers, as in: $1,000,000. These digit separators are there purely for the convenience of readers, providing syntactic cues that make it easier for our brains to process long strings of numbers.

The committee added digit separators to C++ for exactly the same reasons. They won’t affect the evaluation of a number, they are simply present to make it easier to read and write numbers through chunking.

What character to use for a digit separator? Virtually every punctuation character already has an idiosyncratic use in the language, so there are no obvious choices. The final election was to use the single quote character, making the million dollar value render in C++ as: 1'000'000.00. Remember that the separators don’t have any effect on the evaluation of the constant, so this value would be identical to 1'0'00'0'00.00.

An example combining the use of both new features:

#include
<iostream>

 int main()

{

  int val
= 0b11110000;

  std::cout
<<
"Output
mask: "

            <<
0b1000
'0001'1000'0000

            <<
"\n";

  std::cout
<<
"Proposed
salary: $"

            <<
300'000.00

            <<
"\n";

  return 0;

}

This program gives the unsurprising output:

Output mask: 33152
Proposed salary: $300000