Summary of new characteristics of c++14 __c++

Source: Internet
Author: User
Tags function definition

C++14 this new C + + standard after C++11 has been formally approved, is being submitted to the ISO, will be released during the year. Bjarne Stroustrup, the father of C + +, said that although C++14 's improvement was "relatively small" compared to c++11, it still "brought great convenience" to the user and was one of the steps to achieve the goal of making C + + "more friendly to Novices".

In the C + + schedule, c++14 is a small version, completing the remaining work of setting the C++11 standard, to make C + + a clearer, simpler, and faster language. The new language features remain in the C++17 standard of the future.

The main characteristics of c++14 can be divided into three domains: lambda function, constexpr and type derivation. Lambda functions

C++14 's generic lambda makes it possible to write the following statement:

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

On the other hand, c++11 requires a 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 lambda expressions, which are implemented by moving objects rather than copying or referencing objects:

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

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

C++11 only supports type derivation of lambda functions, c++14 expands them to support the derivation of the return type of all functions:

Auto Deducedreturntypefunction ();

Because C++14 is a strongly typed language, there are some limitations to consider: if there are multiple return statements in the implementation of a function, these statements must derive the same type. Return type inference can be used in forward declarations, but before using them, a function definition must be available in the translation unit. Return type inference can be used in recursive functions, but recursive calls must be guided by at least one return statement so that the compiler infers the return type.

Another improvement in the type derivation that c++14 brings is the Decltype (auto) syntax, which supports the use of the same mechanism as auto to compute the type of the given expression. Auto and Decltype are already present in c++11, but they use different mechanisms when deriving types, which may produce different results.

Other changes in c++14 include declaring variable templates and supporting the use of 0b or 0 B prefixes to declare binary literal constants. Infoq has introduced other minor modifications in the 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 everything in the current draft"; GCC and Visual Studio also provide some support for the new features of c++14.

Related Article

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.