BT

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

C++14 Is Here: Summary of New Features

Bookmarks

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

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Your message is awaiting moderation. Thank you for participating in the discussion.

    int lambda = [](int x, int y) {return x + y;};
    its cant working. Only
    auto lambda = [](int x, int y) {return x + y;};

  • Re: Error

    by Sergio De Simone /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Thanks, I have fixed it.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.