C ++ Study Notes (summary of some new features 1)

Source: Internet
Author: User

C ++ Study Notes (summary of some new features 1)
C ++ Study Notes (summary of some new features 1)

Although I have been using C ++ for more than a year, I have always followed the principle that I can only use it. I have not thoroughly studied C ++ syntax, therefore, many advanced C ++ features are unknown. I recently found the book "C ++ 14 Quick Syntax Reference" on the Internet. It is a thin book with more than 100 pages, but covers almost all the features of C ++. This short article is some of the things I did not notice before I read this book.

All the code in this article has passed the gcc version 5.3.0 (Rev1, Built by MSYS2 project) test.

Hexadecimal, octal, and binary representation
int myOct = 062;int myHex = 0x32;int myBin = 0b00110010;

Among them, hexadecimal and octal notation are features that have long been supported. The binary representation is officially supported by C ++ 14.
In addition, C ++ 14 also introduces single quotation marks as the separator for digit representation. It is easy for us to read long numbers. For example:

int longBin = 0b1010'0101'1010'0101;

It is much easier to read by adding three single quotes.

NULL Pointer (nullptr)

In early C ++, we used 0 or NULL to indicate Invalid Pointer addresses. C ++ 11 introduces a new keyword nullptr to indicate NULL pointers.

int* p = nullptr; // ok

In addition, nullptr still has the type nullptr_t:

nullptr_t mynull = nullptr; // ok
Reference Type of the right value (C ++ 11)

Both the left and right values are for expressions. The left value is a persistent object that exists after the expression ends. the right value is a temporary object that no longer exists at the end of the expression.

The reference to the right value is to reference a right value object (temporary object), for example, the following example:

int&& ref = 1 + 2; // rvalue referenceref += 3;cout << ref; // "6"

Ref corresponds to result 3 of 1 + 2, which is a temporary object. The traditional C ++ reference method cannot reference this temporary object. Right-value reference mainly solves efficiency problems. You can search for "C ++ mobile Semantics" in specific methods ".

Native string (raw string literals)

Raw string can cancel escape characters, which is a new feature added in C ++ 11. For example:

string escaped = "c:\\Windows\\System32\\cmd.exe";

Can be abbreviated:

string raw = R"(c:\Windows\System32\cmd.exe)";  

The RAW String must start with an uppercase R, and then a pair of parentheses must be added to the double quotation marks.

This feature has long been available in other languages. The above example does not show the advantages of raw string, but if regular expressions are often written, it will feel that raw string is too convenient. For example:

char str[] = R"(('(?:[^\\']|\\.)*'|"(?:[^\\"]|\\.)*")|)";

The old method is:

char oldstr[] = "('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|";
New char type

The char16_t and char32_t types are introduced in C ++ 11. These two character types can be used to store characters encoded by UTF-16 and utf-32, respectively. The corresponding string type also has two variants: u16string and u32string.

string s3 = u8"UTF-8 string";u16string s4 = u"UTF-16 string";u32string s5 = U"UTF-32 string";

The u8, u, and U before the string are also new features to support UTF-8, UTF-16, and UTF-32 strings, respectively.

For Loop

C ++ 11 extends the for loop, which supports a new syntax.

int a[3] = {1, 2, 3};for (int &i : a) {    cout <
  
Auto and decltype keywords

Both keywords are introduced in C ++ 11. The auto keyword tells the compiler to automatically derive the type of the variable. For example, the following code:

auto i = 5; // intauto d = 3.14; // doubleauto b = false; // bool

If you want to export data of the reference type. Add an & after auto &. For example:

int& iRef = i;auto myAuto = iRef; // intauto& myRef = iRef; // int&

Using auto can simplify a lot of code. For example, the following code:

vector  myVector { 1, 2, 3 };for(vector  ::size_type i = 0; i != myVector.size(); i++) { cout << myVector[i]; }  

If auto is used, it can be written as follows:

for(auto i = 0; i != myVector.size(); i++) { cout << myVector[i]; }

Of course, with the new syntax of for, you can also write more easily:

for (auto& x : myVector) { cout << x << endl; } 

Decltype is similar to auto. It can be used to derive the type of an expression, for example, the following example:

int a = 0b10'001'000;cout << a << endl;decltype(a) c = a + 1; //intcout << c << endl;decltype(3) b = 3; // int&&

In this example, 3 is a temporary variable. Therefore, the type of B is an int-type right value reference. However, when it is returned as a function value, it is not the reference of the right value.

decltype(5) getFive() { return 5; } // int

In C ++ 11, auto and decltype can also be used together to derive the return value type of a function. The following is an example:

auto getValue(int x) -> decltype(x) { return x; } // int

This writing is complicated and simplified in C ++ 14. It can be simply written as follows:

auto getValue(int x) { return x; } // int

However, I don't think these two function writing methods are very useful, because we can't write the function declaration in the header file:

auto getValue(int x);

Because there is no function body, there is no way to deduce the type...

C ++ 14 also supports the following syntax:

decltype(auto) = 3; // int&&decltype(auto) getRef(int& x) { return x; } // int&

These writing methods are fine, and are of little use.

Lambda Functions

The concept of Lambda functions should first come from the Lisp language and is now absorbed by C ++ 11.
Lambda functions allow us to define a function just like a variable. For example:

auto sum = [](int x, int y) -> int {return x + y;};cout << sum(2, 3);

The preceding functions can also be abbreviated:

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

The compiler automatically derives the type of the returned value.

The C ++ 14 Lambda function supports generics.

auto sum = [](auto x, auto y) {return x + y;}; cout << sum(2, 3) << endl;cout << sum(2.2, 3.0) << endl;cout << sum(2.2, 3) << endl;

Lambda functions can also be passed as function parameters. The following is an example:

#include  #include  using namespace std;void call(int arg, function  func) { func(arg);}int main() { auto printSquare = [](int x) { cout << x*x; }; call(2, printSquare); // "4"}   

The above example also shows that Lambda functions are not common functions. It is a special type of object. For example, the following Lambda function:

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

It should be:

function  sum = [](int x, int y) {return x + y;}; 

Lambda functions also have some advanced usage. For example, "[]" in a Lambda function works. It can be used to introduce other variables in the region to the function. For example:

void call(function  func) { func(); }int main() { int i = 2; auto printSquare = [i]() { cout << i*i; }; call(printSquare); // "4"} 

In Lambda functions, the parameters passed by value are read-only. So the following code is wrong:

int a = 10;int b = [a](int i) { a++; return a * i; } (5); // 50

We can add a mutable keyword so that a is not read-only, but changing the value of a will not affect the function.

int a = 10;int b = [a](int i) mutable { a++; return a * i; } (5);cout << b << endl; // 55cout << a << endl; // 10

A Lambda function can also be an unknown function. At this time, you must call this function while defining the function. Otherwise, there will be no chance to call this function because it has no name. The results of the two statements below are the same.

cout << [](int i) { return i*i; } (101) << endl;auto printSquare = [](int i) { return i*i; };cout << printSquare(101) << endl;

You can use "[]" to upload the results. The results of the two methods are the same.

int a = [](int i) { return i * i; } (11);cout << a << endl;[&a] (int i){ a = i * i;}(12);cout << a << endl;

C ++ 14 also supports some new features, such as the following:

int a = 1;[&, b = 2]() { a += b; }();cout << a; // "3"

About Lambda functions, this is almost the same.

The first article first writes so much. The next article describes some new features of classes and objects.

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.