Effective modern C + + reading notes

Source: Internet
Author: User
Tags shallow copy

Type derivation:

1.ParamType is reference or pointer, not universal reference

References are ignored, the remainder matches T

Template <typenmae t>void f (t& param); int x = 27;const int cx  =x;oonst int& rx = x;f (x);//t is int, para Mtype is int&f (CX);//t are const int, paramtype is coint int&f (RX);//t are const int, Paramtype is Coint int&
2.paramType is universal reference

1) If Lvalue,t is deduced as a reference, Paramtype is also quoted

2) If it is rvalue, use the 1 rule

Template<typename t>void f (t&& param); int x = 27;const int cx = x;const int & rx = x;f (x);//x is Lvalue, T is Int& Paramtype are int&f (CX);//x is lvalue, T is const int&, Paramtype are const int&f (RX);//x is Lva Lue, T is const-int&, Paramtype is const int&f ();//27 are rvalue, T is int., Paramtype is int&&
3. Non-reference, non-pointer

There are reference in expr, ignored, const, ignored, volatile, ignored.

Template<typename t><pre name= "code" class= "CPP" >void F (T param); int x = 27;const int cx = x;const int & R x = x;
All of the following T Paramtype are intf (x);
F (CX);
F (RX);

The derivation rules for auto are basically the same as the template, except for the following:

Auto is initialized to std::initializer_list when initializing braced initializer, and the template type deduction does not

Auto x = {n/a}; Type is std::initializer_list<int>

Tips

Auto Returns or lambar the parameter as a function the way the template type deduction.

In addition, the use of auto, especially when initializing the object must be type-specific time.




Modern C + +:

7. Distinguish between () when creating an object, {}

e.g:

Std::vector A (3,0);  Create 3 0 vectorstd::vector b{1,2,3};//to create a vector


8-15: Keywords that use the new standard

Nullpter instead of 0,

Use using instead of TypeDef,

Use Delete to delete unnecessary functions.

Use override to indicate function overrides,

Using Const_iteratorter instead of iterator,

Using noexcept when there is no exception to the function,

Functions that can be compiled with constexpr to the compiler


16. Making a function thread-safe

C++11 has a new memory model that supports multithreaded programming


17. Understanding compiler-generated functions

6 functions: Default constructor,destructor,copy operations (copy constructor,copy assigment), move operations

Move operation is only generated when the class lacks move operation,copy operation and destructor

Copy constructor is only generated when there is a missing explicit copy constructor and is deleted when the move operation declaration.

Copy assignment Opetaor is only generated when there is a lack of explicit copy assignment Opetaor, and is deleted when the move operation declaration.

Function templates cannot prevent special function generation


Smart pointers:

Exclusive resources use STD::UNIQUE_PTR, shared resources using STD::SHARED_PTR, with std::shared_ptr use std::weak_ptr;

Use std::make_unique,std::make_shared instead of creating objects (not violating the principle of new,delete in pairs), and also to these two functions as arguments to the object being created.

Be careful about circular references.

Auto_ptr copy is a shallow copy, pointing to the bottom of the area is the same block.


Rvalue semantics, moving semantics, perfect forwarding:

Std::move convert an object to Rvalue;std::forward to rvalue only if the parameter is bound to an rvalue and the other is an lvalue; Std::move,std::forward are compiler conversions

Universal reference refers to the object satisfies the type derivation and the &&,Universal reference to rvalue only when the parameter is rvalue, otherwise all is lvalue;

Std::move with rvalue use, std::forward with universal reference use;

< Span style= "Font-family:helvetica, ' Hiragino Sans GB ', Microsoft Ya Hei, ' Microsoft Yahei UI ', Simsun,simhei,arial,sans-serif ' >< Span style= "Font-family:helvetica, ' Hiragino Sans GB ', Microsoft Black, ' Microsoft Yahei UI ', Simsun,simhei,arial,sans-serif" > Universal reference use avoid overloading

< Span style= "Font-family:helvetica, ' Hiragino Sans GB ', Microsoft Ya Hei, ' Microsoft Yahei UI ', Simsun,simhei,arial,sans-serif ' >< Span style= "Font-family:helvetica, ' Hiragino Sans GB ', Microsoft Ya Hei, ' Microsoft Yahei UI ', Simsun,simhei,arial,sans-serif ' >< Span style= "Font-family:helvetica, ' Hiragino Sans GB ', Microsoft Ya Hei, ' Microsoft Yahei UI ', Simsun,simhei,arial,sans-serif ' >< Span style= "Font-family:helvetica, ' Hiragino Sans GB ', Microsoft Ya Hei, ' Microsoft Yahei UI ', Simsun,simhei,arial,sans-serif ' >e.g:

Templte<typename t>void Logandadd (t&& name) {...}; void Logandadd (int idx) {...}; Logandadd (a);   CALs int Overloadshort Nameidx;logandadd (NAMEIDX);  Error

And the solution to this scenario: pass by Lvalue-reference-to-constant, pass by value, tag Dispatch, discard overload between options

reference Collapsing: The compiler generates a reference TI a reference, and the result becomes a separate reference; only two of them are rvalue. Reference will get rvalue reference, otherwise they are lvalue reference

What happens when the compiler is generated:

Template<typename t>void func (t&& param); Widget widget (); Widget W;func (w);//call func with Lvalue;func (Widgets ());//call func with Rvalue
Familiarity with Std::forward failure scenarios, such as braced initializers,0 or null or null pointers, overloaded functions and template functions, Bitfield

Lambda expression:

Avoid default captures, set & or = for each capture parameter;

& when capturing, the value of an externally captured object must be greater than the closure in its lifetime. Otherwise, when a closure call occurs, the object is already destructor;


Entering closures using the Init capture move parameter

Auto Func = [PW = std::make_unique<widget> ()]{...};

For use with auto&& and Std::forward (c++14)

C + + Multithreading:

Choose task-based better than thread-based. Tasks are usually independent, reducing the association between threads and threads, reducing the use of thread synchronization and naturally easier to use

Std::launch::async keyword use but asynchronous when necessary, launch's introduction

Std::thread are unjoinable on all paths, either join to threads or detach

Use Std::future to override conditional variables (advanced synchronization primitives instead of basic synchronization primitives)

Using Std::atomic,volatile to decorate multithreaded shared variables (the latter makes the compiler not optimized, Atomic's introduction

Miscellaneous:

When a class adds an object to a private container, the Lvalue and rvalue parameters are processed:

1. Using the overloaded function, one using the Lvalue parameter, one using the Rvalue parameter

2. Using Universal Reference

3. Transfer value

The consumption of three scenarios:

1. One copy to Lvalue, one move to Rvalue

2. Above

3. In the function weight, the value parameter is move into the container, so move more than one of the above two, whether lvalue or rvalue


Use the Emplacement series function instead of inserting

1. Reduced operation on the way, more efficient

The emplace of vectors

Vectoe's Emplace_back



Effective modern C + + reading notes

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.