Summary of new features in C++11

I. Overview

C++11 new features include auto, decltype, constexpr, rvalue reference, move() function, perfect forwarding, move construction, lambda anonymous function, new format for loop, nullptr, shared_ptr, weak_ptr, unique_ptr, using definition alias, function Template default arguments, tuple, unrestricted union, placement new, longlong int, etc.

2. Text

The following is a summary of the notes I recorded during the learning process. The actual code is less, and most of them are knowledge points, such as related features, definitions, limitations, functions and descriptions, etc., put half of the content, and the remaining half is at the bottom of the article. The resource department is really a concise summary of personal intentions, thank you for your support and understanding~~

  • autotype deduction
    • application
      1. define iterator
      2. generic programming
    • limit
      1. Variables declared by auto must be initialized by assignment, so auto cannot be used in function parameters, nor can it be applied to non-static class member variables, because class members cannot be assigned values ​​when they are declared.
      2. The auto keyword cannot define an array
      3. auto cannot be used as template parameter
    • nature
      1. auto is just a placeholder, it is replaced by the real type during compiler time
      2. Derivation must not be ambiguous
      3. When the type is not a reference, the deduction result of auto does not retain the const attribute of the expression, otherwise it retains
    • Introduction: Before C++11, auto means that variables are automatically stored, which is also the default rule of the compiler, so writing it or not is the same; after C++11, auto is used for automatic type push, and after the keyword, compile The compiler automatically deduces the type of the variable during compilation.

  • decltype(declare type) Basic usage: decltype(exp) var;
    • return value type
      1. In order to solve the problem that the return value type of the function depends on the parameters and it is difficult to determine the return value type
      2. combined with auto decltype
    • Difference from auto
      1. auto deduces according to assignment, decltype deduces variable type according to expression
      2. auto requires variables to be initialized, while decltype does not
    • derivation rules
      1. If it is an ordinary variable or class member access expression, the deduced type is the same as exp
      2. If it is a function call, it is the same as the return value
      3. If it is an lvalue or a parenthesized expression, a reference of type exp is deduced
    • practical application
      1. Type deduction for class members
  • Support for function template default template parameters (previously only class template default template parameters were supported)
  • tuple
    1. Features: An instantiated object can store any amount of data of any type
  • List initialization (uniform initialization format)
  • lambda anonymous function
    1. Format:

      [external variable orientation mode specifier] (parameter) mutable noexcept/throw() -> return value type

      {

              function body;

      };

    2. illustrate
      1. [External variable mode orientation specifier]: [] is used to declare that the current is a lambda expression, and the square brackets indicate which external variables can be used in the current function body
      2. (parameter): It is used to pass parameters like ordinary functions. If no parameters are received, it can be omitted.
      3. mutable: External variables introduced by value passing are constants by default. To modify them, you need to use the mutable keyword, which can be omitted
      4. noexcept/throw(): By default, a lambda function can throw any type of exception. If noexcept, no exception will be thrown. If throw() can specify the type of exception thrown, it can be omitted
      5. ->Return value type: Specify the return value type of the lambda anonymous function. If there is only one return in the function body or the function returns void, it can be omitted
      6. Function body: You can use not only parameters passed by value, but also specified external variables and all global variables in the global scope. External variables will be affected by whether they are passed by value or by reference, while global variables will not. lambda expression You can use and directly modify the value of global variables within the formula
    3. Application example
      1. Simplest anonymous function: []{};
      2. Sort array sort(num.begin(), num.end(), [=](int x, int y) -> x > y);
      3. Name the anonymous function auto display = []{};
  • using define alias
    1. Function: Rename a type or function template, and add the function of template renaming on top of typedef
  • unrestricted union
    1. Non-POD types are allowed in C++ unions
    2. POD type
      1. Does not contain virtual functions and virtual base classes
      2. Non-static members must be declared public
      3. No user-defined destructor, constructor, and copy functions
    3. placement new
      1. An advanced usage of the new keyword, which can generate objects on the heap or on the stack
      2. Syntax: new(address) ClassConstruct(...); adress represents the existing memory address, which can be a reference to a variable, ClassConstruct represents the constructor of the calling class

Finally, the resource link of all the content is released, thank you~~ 

 Practical summary of new features of C++11 word version-C++ document resources-CSDN download is mainly the definition, features, usage restrictions, etc. of new features of C++11. There is not much code on how to use the new features, most of them are knowledge For more download resources and learning materials, please visit the CSDN download channel. https://download.csdn.net/download/weixin_44178960/85018835

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324155398&siteId=291194637