BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News C++14 Is Here: Summary of New Features

C++14 Is Here: Summary of New Features

This item in japanese

C++14, the new C++ standard succeeding C++11, has been finally approved and is heading to ISO for publication this year. While improvements in C++14 are "deliberately tiny" compared to C++11, says C++ creator Bjarne Stroustrup, they still "add significant convenience for users" and are a step on the route to make C++ "more novice friendly."

Within the C++ timeline, C++14 was planned as a minor release to complete the work that produced the C++11 standard, with the aim of becoming a cleaner, simpler, and faster language. New language features are left for the coming C++17 standard.

Major C++14 features can be grouped in three areas: lambda functions, constexpr, and type deduction.

Lambda functions

C++14 generic lambdas make it possible to write:

auto lambda = [](auto x, auto y) {return x + y;};

On the other hand, C++11 requires that lambda parameters be declared with concrete types, e.g:

auto lambda = [](int x, int y) {return x + y;};

Furthermore, the new standard std::move function can be used to capture a variable in a lambda expression by moving the object instead of copying or referencing it:

std::unique_ptr ptr(new int(10));
auto lambda = [value = std::move(ptr)] {return *value;};

Constexpr

A constexpr-declared function in C++11 is a function which can be executed at compile time to produce a value to be used where a constant expression is required, such as when instantiating a template with an integer argument. While C++11 constexpr functions could only contain a single expression, C++14 relaxes those restrictions by allowing conditional statements such as if and switch, and also allowing loops, including range-based for loops.

Type deduction

C++14 allows return type deduction for all functions, thus extending C++11 that only allows it for lambda functions:

auto DeducedReturnTypeFunction();

Since C++14 is a strongly-typed language, a few restrictions shall be taken into account:

  • If a function's implementation has multiple return statements, they must deduce the same type.
  • Return type deduction can be used in forward declarations, but the function definitions must be available to the translation unit that uses them before they can be used.
  • Return type deduction can be used in recursive functions, but the recursive call must be preceded by at least one return statement allowing to deduce the return type.

Another improvement to type deduction brought by C++14 is the decltype(auto) syntax, which allows to compute the type of a given expression using the same mechanism as auto. Both auto and decltype were already present in C++11, but they used different mechanisms to deduce types that could end up in producing different results.

Other changes in C++14 include the possibility of declaring variables that are templated and using binary literals specified using the prefixes 0b or 0B. InfoQ has already covered other minor changes in C++14 that could break C++11 programs.

Support for the new language features is well underway on major C++ compilers: Clang "fully implements all of the current draft"; GCC and Visual Studio also offer some support for C++14 new features.

Rate this Article

Adoption
Style

BT