A brief summary of new features of c++11 (i) _c language

Source: Internet
Author: User
Tags int size

What is c++11

C++11 was once called c++0x, is the extension and revision of the current C + + language, c++11 not only contains the new functions of the core language, but also extends the standard library of C + + (STL), incorporating most of the C + + technical 1 (TR1) library ( Except for the special functions of mathematics).

C++11 includes a number of new features: lambda expressions, type derivation keywords auto, decltype, and templates for a lot of improvements.

1. Overview

Recently in see C++ Primer5 just see half, summed up c++11 inside did add a lot of new things, if there is no understanding, don't say oneself write, see others write code estimates will be a bit laborious. C++ Primer5It is a better book to learn c++11. This article only sums up new things about c++11, and the old things don't repeat themselves. All the code in this article lists key code, and all features have been validated by the compiler, and my compilation environment, GCC 5.3.1 g++ 5.3.1, says that more than 4.7 of the versions already support most of the c++11 features, and the VS series compiler has little knowledge of c++11 support. If there is no suitable compiler, you can click here C++shell this is an online C + + compiler system, which has multiple options to choose C++98,c++11,c++14, which can be used to verify the correctness of c++11.

2 Long Long type

long long The type does not actually exist in C + + 98, and is later included in the C99 standard, where most compilers support long long, but this type formally becomes the standard type of C + + in c++11. The standard requires a long long of at least 64 bits, which is 8 bytes. A literal constant uses the ll suffix to represent a long type, using the ull suffix to represent the unsigned long long type.

3. List initialization

List initialization is fully added to the c++11, including Vector,map, value types, struct, and so on, which can be initialized with lists, and a list of curly braces can be returned in the function, before which we can only initialize the list to the array:

Array list initialization
int xx[5]={1,2,3,4,5};
int yy[]={6,7,8,9,0};
 
The value type initializes
int a{10};
int b={10};
int c={10.123}; Compiler error, g++ 5.3.1 when the list is initialized for value types, the compiler will complain if there is a loss of precision.
 
//List initialization can also be
struct str{int x with the structure typedef
  ;
  int y;
} STR;
Str s = {10,20};
 
List initialization class, must be public member, if contains private member will fail
class cls{public
:
  int x;
  int y;
};
Cls c = {10,20};
 
Vectors can not only use list initialization, but also use lists to assign values, arrays cannot be
vector<int>v1={1,2,3,4,5,6,7,8,9};//initialized with lists
vector< int>v2;
v2={3,4,5,6,7}; The assignment
 
//map list initializes
map<string,int> m = {
   {"x", 1},
   {"Y", 2},
   {"Z", 3}
};
 
Returning an initialization list with a function only shows the key code, the associated header file is added to the
///empathy structure, the class, the return of the map can also use the initialization list to return
vector<int> getvector ()
{
 return {1,2,3,4,5};
}
 
int main ()
{
 vector<int> v = getvector ();
 Cout<<v[0]<<v[1]<<v.size () <<endl;
 return 0;
}

4. NULLPTR NULL pointer

The newly added literal value in c++11 represents a null pointer that does not point to any object, and we used to use a predefined macro null to represent the null pointer, actually NULL is 0, and the new standard recommends using nullptr instead of NULL

5. Constexpr variable

We usually define constants by using the const definition, a constant must be initialized at the time of definition, and cannot be changed after that. A constant must be initialized with a constant expression, and the value of the constant can be obtained during compilation, but how to determine that an expression is a constant expression is usually determined by the programmer itself, for example:

const int a =20;
20 is a literal, of course, a constant expression, so it is no problem to assign a value with 20来
//However the following code can also be compiled, g++ 5.3.1
int a =;
const int x = A;
int b[x]={0};

Assigning a value to a constant x is a variable a, which should be unreasonable, however, the compiler does not report any errors, of course, this error is obvious, but in a complex system how to determine whether an expression is a constant expression is very difficult, for example, here a we can tell at a glance that it is not a constant expression. This c++11 provides a new keyword CONSTEXPR, using constants defined by the keyword, that the compiler checks whether the expression assigned to it is a constant expression, such as the above code:

int a =;
constexpr int x = A;

When the compiler compiles, it complains that a is not a constant. Obviously the CONSTEXPR keyword will pass the check of the constant expression to the compiler instead of the programmer itself, so it is safer to define constants using constexpr than Const.

6. constexpr function

Ordinary functions are generally not used to assign values to constexpr constants, but c++11 allows the definition of a constexpr function that can be computed as a result during compilation, and such functions can be used to assign values to constexpr. Defining the CONSTEXPR function requires compliance with conventions, the return type of the function, and the type of all formal parameters should be literal values, and in general the function body must have and have only one return statement.

constexpr int size ()
{return
  ;
}
 
constexpr int si = size ();

When initialization is performed, the compiler replaces the invocation of the function with the result value, and other statements other than return are available in the CONSTEXPR function body, but they should not perform any actions at run time, such as null statements, using declarations, and so on. The CONSTEXPR function allows it to return a value that is not a literal, for example:

constexpr int size (int s)
{return
  s*4;
}
 
int a =;
const int b =;
constexpr int c =;
constexpr int si = size (a); Error A is a variable so the function returns a variable value
constexpr int si1 = size; The//ok function returns actually a constant
constexpr int si2 = size (b);//ok< c16/>constexpr int si3 = size (c); Ok

The CONSTEXPR function does not necessarily return a constant if the parameter applied to the function returns a constant if it is a constant expression, otherwise the variable is returned, and whether the function call is a constant or a very literal expression is judged by the compiler. This is the benefit of constexpr.

7. Using Type Alias

The type alias is actually early in the C language, and in general we use the keyword typedef to declare a type alias, and another way to declare the type alias in c++11 is to use the Using keyword, which is used to refer to namespaces before c++11.

typedef int INT; The right symbol represents the left
using INT2 = int;//The left symbol represents the right
 
int a =;
INT2 B = 30;

8. Auto Type Indicator

When we define a variable, we first have to determine the type of the variable, and a lot of times it's not that we need a variable and then assign the appropriate data to the variable, but we have a value but we don't know what type of variable to store it in, especially the template for C + + is used very extensively, Sometimes to define a variable, the type is complex with the type parameters of the template, such as one of the most common examples:

Map<string,int> m;
Map<string,int>::iterator it = M.begin ();

In the example above we defined a map<string,int>::iterator type of variable to hold the value of M.begin (), which is relatively not difficult but I was also dizzy when I started using the map container if map was a The map<string,double> type needs to be defined as a map<string.double>::iterator it to be stored. In particular, it is easier to make a mistake if the map and vector are nested between each other. Another drawback to defining this type of variable is that a type of name tends to be very long, and if you think of the variable declarations throughout the program code, it's unlikely that a few people will look comfortable. But it's okay. C++11 defines a new keyword auto to define the variable, and the type of the variable is automatically deduced by the compiler based on the expression of the assignment, without the need for us to display the definition. Because the type of an auto-defined variable is inferred by the compiler based on the expression of the assignment, the auto-defined variable must have an initial value or the compiler cannot determine the type of the variable.

Auto x = 20; X is int
auto y = 3.14;//y is double
map<string,int> m;
Auto it = M.begin (); It is Map<string,int>::iterator

This is a lot easier, and the program looks more concise
Auto can declare multiple variables in one statement, but make sure that there is only one underlying data type in the statement, for example:

Auto i=10,*p=&i; OK i is int,p is int*
auto a=10,b=3.14;//Error type int or double?

9. Decltype Type Indicator

Sometimes there is such a requirement that we need to know the type of an expression and use that type to define a variable, for example:

int a = ten;
int b =;
Auto C = a + B; The type of OK a+b is int, at which point the type of C is int, and the value of C is a+b

Auto can solve some of the problems, such as the type of the variable we define is the type of the expression a+b, but if we just need to define a variable that is the same type as the expression a+b, we do not want to assign the value of the expression a+b to the variable just defined. We want to assign another value or just define the variable without assigning a value. This requires the use of another type descriptor Decltype provided by C++11. Decltype acts on an expression and returns the type of the expression, in which the compiler analyzes the type of the expression and does not evaluate the value of the expression. For example

int a = ten;
int b =;
Decltype (a+b) c = 50; The type of OK C is the a+b type int

There are some special places for reference type Decltype:

int a =;
int &b = A;
Decltype (b) C; Error c is a reference type that must be assigned
Decltype (b) d = A;//OK D is a reference type, pointing to a

You can see that decltype if it acts on a reference type, it gets a reference type. We know that a reference type is used in general as a synonym for the variable to which it is associated, for example, if you use the cout<<b<<endl; where B is actually equivalent to a, but Decltype retains the reference property when it acts on a reference type.

If an expression is an operation referenced by a pointer, Decltype is also given a reference type:

int a =;
int *p = &a;
Decltype (*p) c = A; The type of C is int&
C = m;
cout<<a<<endl; Output 50

When Decltype is acting on a variable, there is a difference between the addition of a variable and the parenthesis, for example:

int a =;
Decltype (a) b = 30; The type of OK B is int
decltype ((a)) c = A;//OK C is the type of int& its associated variable a

With parentheses, the compiler treats (a) as an expression, and the variable is an expression that can be the left value of the assignment statement, so it is interpreted as a reference type.

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.