Summary of new features in C + + 14

Source: Internet
Author: User

Reprinted from: http://www.codeceo.com/article/cpp-14-new-features.html

C++14 this new C + + standard following c++11 has been formally approved and is being submitted to ISO for release within the year. Bjarne Stroustrup, the father of C + +, said that although C++14 's improvements were "intentionally small" compared to c++11, it was "a great convenience" for the user and one of the steps to achieve the goal of making C + + "more friendly to newbies".

In the C + + schedule, c++14 is a small version of the plan, completing the remainder of the C++11 standard to make C + + a clearer, simpler, and faster language. The new language features are reserved for future c++17 standards.

The main features of c++14 can be divided into three areas: Lambda functions, constexpr, and type derivation.

LAMBDA functions

C++14 's generic LAMBDA makes it possible to write the following statements:

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

On the other hand, C++11 requires the LAMBDA parameter to use a specific type declaration, such as:

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

In addition, the Std::move function in the new standard can be used to capture variables in a LAMBDA expression, which is implemented by moving objects rather than copying or referencing objects:

Std::unique_ptr ptr (new int); auto Lambda = [value = Std::move (PTR)] {return *value;};

constexpr

In c++11, functions declared with constexpr can be executed at compile time, generating a value that is used where constant expressions are required, such as the shaping parameters of the initialization template. C++11 's constexpr function can contain only one expression, c++14 relaxes these limits, supports conditional statements such as if and switch, and supports loops, including a For loop based on interval (range).

Type deduction

C++11 only supports type inference for LAMBDA functions, and c++14 extends them to support the return type derivation of all functions:

Auto Deducedreturntypefunction ();

Because C++14 is a strongly typed language, some limitations need to be considered:

    • If there are multiple return statements in the implementation of a function, the statements must derive the same type.
    • Return type deduction can be used in forward declarations, but before they are used, the function definition must be available in the translation unit.
    • Return type deduction can be used in recursive functions, but recursive calls must be piloted with at least one return statement so that the compiler infers the return type.

Another aspect of the type deduction that C++14 brings is the Decltype (auto) syntax, which supports the calculation of the type of a given expression using the same mechanism as auto. Auto and Decltype already appear in c++11, but they use different mechanisms when deriving types, which can produce different results.

Other changes in c++14 include the ability to declare variable templates and support the use of 0b or 0 B prefixes to declare binary literal constants. InfoQ has described other minor changes in c++14 that could disrupt the C++11 program.

The mainstream C + + compiler's support for new language features is being methodically developed: Clang "fully implements all the content of the current draft"; GCC and Visual Studio also provide some support for the new features of c++14.

Summary of new features in C + + 14

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.