Components in tr1-smart pointers and functions

Source: Internet
Author: User

I. smart pointer

Tr1: shared_ptr and tr1: weak_ptr. The former serves as a built-in pointer, but records the number of TR: shared_ptrs pointing to the same object. This is the so-called reference count. Once the last pointer is destroyed, the object will be automatically deleted once the reference count of an object is 0. This is helpful to prevent resource leakage in non-circular data structures, but if two or more objects contain tr1: shared_ptrs and form a ring, this ring will cause the application count of each object to exceed 0-even if all the pointers pointing to this ring have been destroyed (that is, this group of objects seems inaccessible as a whole ). That's why there is another tr1: weak_ptrs. The Design of tr1: weak_ptrs makes it look like a inductive pointer in a non-circular tr1: shared_ptr-based data structure. Tr1: shared_ptr is not involved in the calculation of application count. When the last tr1: shared_ptr pointing to an object is destroyed, even if there is another tr1: weak_ptrs pointing to the same object, this object will still be deleted. In this case, the tr1: weak_ptrs will be automatically marked as invalid.

Tr1: shared_ptr may be the most widely used tr1 component,

Ii. tr1: Function

This object can indicate any callable object, that is, any function or function object. As long as the signature matches the target, if we want to register a callback function, this function accepts an int and returns a string, we can write as follows:

Void registercallback (STD: String func (INT); // The parameter type is a function. This function accepts an int and returns a string

The parameter name func is optional, so the preceding registercallback can also be declared as follows:

Void registercallback (STD: string (INT); // The same as the preceding one. The parameter name is omitted but not written. In this example, "STD: string (INT) "is a function signature. Tr1: function makes the preceding registercallback more flexible to accept any callable object. Only this callable object accepts an int or anything that can be converted to an int, returns a string or anything that can be converted to a string. tr1: function is a template and takes the signature of the target function as the parameter,

Void refistercallback (STD: tr1: function <STD: string (INT)> func );

// The parameter "func" accepts any callable object as long as the signature of this "callable object" is consistent with "STD: string (INT )"

3. tr1: bind

It can be used as the STL bindlst and bind2nd bindings. What is different from the former one is that tr1 :: binf can work with const and non-const member functions, and can work with the by-reference parameter. It can process function pointers without special assistance, so we call tr1 :: before bind, you don't have to be confused by ptr_fun, mem_fun or mem_fun_ref. In short, tr1: binf is the second generation binding tool, which is much better than the previous generation.

(The following is a brief introduction to the usage of the above three components)

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.