Auto and decltype

What problem to solve

The main purpose of generic programming is to solve the problem that some types are difficult to represent by template parameters

keywords

  • Auto in the compiler deduces the type of the variable from the type to the right of =
  • Decltype Specifies the type of the expression derived by the compiler
  • Auto and decltype are used together to derive the type of the return value of a function
Template <typename T, typename U> //return_value add(T T, U U) {return T + U; Auto add(T T, U U) -> decltype(T + U) {return T + U; }Copy the code

Smart Pointers

What problem to solve

Use RAII(resource fetch as initialization) to solve memory problems caused by coding such as memory leaks, double free, etc

keywords

C++ 11 introduces three types of smart Pointers:

  • STD ::shared_ptr Multiple objects simultaneously point to the same memory
  • STD :: Weak_ptr acts as a bystander to monitor the existence of managed resources in shareD_Ptr and solve the circular reference problem
  • STD ::unique_ptr An object occupies a chunk of memory

STD: : the lock