C++11 new Features

Source: Internet
Author: User
Tags arithmetic define null volatile

1.auto Type deduction

In earlier versions, the keyword Auto was primarily used to declare local variables with an automatic storage period. That is, a local variable, except for the static type, the other variable (defined as data type + variable name) defaults to having an automatic storage period, so the Auto keyword is optional.

In the c++11 version, the original function of auto was removed and redefined. That is, auto in c++11 has the function of type deduction.

A static type is the data type of the variable that needs to be defined before the variable is used

Dynamic types do not need to define data types for variables

A static type is a type check at compile time, while a dynamic type is a type check at run time.

Auto is equivalent to a placeholder for a type declaration, not a "type" declaration, and the compiler replaces auto with the actual type of the variable at compile time.

The advantages of Auto

Simplified code when declaring a type variable. Also avoid some errors when declaring a type. The most common is the type of the iterator

std::vector<std::string> Array;std::vector<std::string>::iterator it = Array.begin (); Auto it = Array.begin ();

When you want to avoid defining an integer type when you need a double type, auto is deduced when you use Auto.

Auto can not be used to declare function parameters, which seems to be supported in c++14.

Auto Limit

Auto can infer the pointer automatically, but cannot infer the reference automatically

Equivalence definition auto* A = &x;auto a = &x//fee equivalent, first is value semantics, the second is reference auto A=x;auto &a=x;

Auto will automatically remove const (constant), volatile (volatile).

For references and pointers, that is, auto*, auto& will still remain const and volatile.

When you define multiple variables in a row, all variables are of the same type, and are the type of the first variable, or the compilation error occurs.

A variable type used to declare dependent template parameters when defining a template function. Is the operation of the formal parameter, the result is unknown before compiling, you can use auto to push to. Similarly, the return value that relies on template parameters is the same.

The auto variable must be initialized at the time of definition, similar to a reference.

If the initialization expression is a reference, the referential semantics are stripped.

If the initialization expression is const or volatile (or both), the const/volatile semantics are removed.

If the Auto keyword is with the & number, the const semantics are not removed.

When the initialization expression is an array, the AUTO keyword deduction type is a pointer.

function or template parameter cannot be declared as auto

Auto is just a placeholder, it's not a real type, and you can't use some operators with the type operand, such as sizeof or typeid.

2 Decltype Type deduction

Decltype () when there is an object or type, the type is deduced from the object or type and used to create the object. Or, yes. After the type continues to add a reference, or a pointer, so that the corresponding assignment needs to match the deduced type

Therefore, it can be used as a function parameter because the object that he needs is already typed, not just out of thin air.

Type deduction is introduced with the extensive use of templates and generic programming. In non-generic programming, the type is unambiguous, and in the template and generic programming, the type is ambiguous, depending on the type of parameter passed in.

Decltype and I talked about the auto still have some in common, if both are derived by the derivation of the type to define another variable, and if both are at compile-time type deduction. However, their type derivation is different, auto is derived from the initialization of the expression type, and Decltype is derived from the normal expression of the return value of the type.

typeID and Decltype

Run-Time type identification (RTTI),

Rtti mechanism: To generate a type_info type of data for each type, programmers can use typeID in the program to query the type of a variable at any time, typeID will return the corresponding type_info data of the variable Type_ The name of the info member can return the type's names. In C++11, the Hash_code member function is added to return the unique hash value of the type for the programmer to compare variable types at any time.

Directly to the Type_info.name string comparison is not enough, why also give each type a hash value? I think the cost of string comparisons is also larger, and if you use each type for a hash value, comparing the hash value to determine whether the type is the same is much more efficient than using string comparisons.

typeID (b). Name () typeid (a). Hash_code () = = typeID (b). Hash_code ()

Rtti determines the type at run time, and more requirements are determined at compile time. Also, the usual procedure is to use this type of derivation rather than simply identifying it.

Using size_t = decltype (sizeof (0));

The four rules deduced by Decltype are as follows:

    1. If E is a token expression with no parentheses or a class member access expression, then Decltype (e) is the type of the entity named E. In addition, if E is an overloaded function, it may cause compilation errors;
    2. Assuming that the type of E is T, if E is a dead value (Xvalue), then Decltype (e) is t&&
    3. Suppose that the type of E is T, and if E is an lvalue, then Decltype (e) is t&;
    4. Assuming that the type of E is a T, then Decltype (e) is T.

The difference between 3,4 is that 3 is the result of an operation. and 4 is a single variable, no operation

For example

int I=1;decltype ((i)) a=0; () arithmetic, is Int&decltype (++i) b=1; ++i is the result of an operation. inferred as int &decltype (i++) c = 1; First infer I, then self-increment, therefore is Intdecltype (arr[1]) d=1; The result of the operation, and therefore the reference.

For the first, for the array type, Decltype can deduce the complete type of int [10].

Decltype can "take away" the CV-limiter of the expression. However, if there is a CV-limiter in the definition of an instance of an object, the members of its instance do not inherit the const or volatile limiter.

# include <iostream># include <type_traits>using namespace std;const int ic = 0;volatile int iv;struct S {
   
    int i;};  Const S A = {0};volatile s b;volatile s* p = &b;int Main (void) {    cout << is_const<decltype (IC) >::value << Endl; 1    cout << Is_volatile<decltype (iv) >::value << Endl;//1    cout << is_const< Decltype (a) >::value << Endl; 1    cout << is_volatile<decltype (b) >::value << Endl;//1    cout << is_const<decltype (A.I) >::value << Endl; 0    cout << is_volatile<decltype (p->i) >::value << Endl;//0    return 0;}
   

Template, but when the return value is related to the parameter, such as the type after the parameter is referenced, C++11 introduces a new syntax-tracing the return type to declare and define such a function

Template<typename T1, TypeName T2>auto Sum (t1& t1, t2& T2)->decltype (t1+t2) {    return t1+t2;}

3 Range for

The range-based for loop, combined with Auto's keyword, requires programmers to know that "I iterate through every element", and no longer have to care about the scope, how to iterate over the details of the access.

If the range for uses auto inference type, then auto does not automatically infer the reference type, such as the need to add &

4 nullptr

Typically there are two types of initialization pointers: 0 and NULL, intended to indicate that the pointer is pointing to an empty position.

Null is actually a macro definition

stddef.h# undef null# if define (_cplusplus) # define NULL 0# else# define NULL ((void*) 0) # endif

Null can be replaced either as Integer 0 or as pointer (void*) 0. This may cause some problems, such as ambiguity:

When overloading a function, if the overloaded function accepts an int type and accepts the void* type, then passing in NULL will cause an error.

nullptr_t is a type of nullptr, called a pointer null value type.

All data defined as nullptr_t types is equivalent and behaves exactly the same.

Data of type nullptr_t can be implicitly converted to any pointer type.

nullptr_t type data can not be converted to non-pointer type, that is, the use of reinterpret_cast () can not be converted; can be strong, but the error indicates the accuracy loss, plus the compilation parameters, strong transfer warning, the value of 0

An object of type nullptr_t does not apply to an expression of arithmetic operations;

nullptr_t type data can be used for relational operation expressions, but can only be compared with nullptr_t type data or pointer type data, and returns true only if the relational operator is-=, <=, >=, and so on.

Null and (void*) 0

Nullptr is a compile-time constant, whose name is a compile-time keyword that can be recognized by the compiler, while (void*) 0 is only an expression that enforces type conversions, and its return value is also a void* pointer type.
Nullptr can be implicitly converted to pointers, whereas (void*) 0 can only be displayed for conversion to be pointer type (C++11). Although pointers in the C + + standard (void*) type can be implicitly converted.

6 __cplusplus

Used with the compiler to determine what compiler to use.

This macro is automatically defined when using a C + + compiler, such as g++ clang++. But the compiled program needs to be called by C, or C. It needs to be compiled to C.

So use this macro

Code Start:
#if defined (__cplusplus) | | Defined (c_plusplus) extern "C" {#endif

End of code:
#if defined (__cplusplus) | | Defined (c_plusplus)} #endif

7 Alignment

C++11 supports alignment in any way

Alignas (8) Char c[1024];

It can also be a type in parentheses, and then infer the byte size of the type by type.

Alignof can return the size of the alignment

int  i = alignof (a);

8 The default or deleted construct series function 9 delegate constructor is displayed

10 Display conversion operator 11 initialization list lambda function

C++11 new Features

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.