C++11 template class Usage experience

Source: Internet
Author: User

1. It is recommended to use std::shared_ptr<taskt> instead of pointer taskt*, shared_ptr is a smart pointer, can be self-destruct free memory, introduced in C++11, in multithreaded programming has a great use, It can count how many pointers point to the same object. This class is included in the <memory>.

2. In a template class to use the iterator type of a container, the iterator type needs to be preceded by a typename to compile for an unknown reason.

3. As long as the iterator type in the container is not deleted, the content pointed to by iterator is constant, which allows for many callbacks to be deleted, while void* can point to the iterator address and can be used by the void* pointer callback.

4.typedef cannot redefine the template class, can redefine the determined type, how to redefine the type of template class, there is no good solution, seemingly can use template alias?

5. With regard to the use of functions in a template class, if it is such a function void cmp (int a,int b) points to him the function is function<void (int,int) > Func. The template class can redefine () to call a function through the class name, which generally requires a func (A, B).

6. For lamda expressions, for void cmp (int a,int b) {return a<b;} Can be written as an expression [] (int a, int b) {
Return a<b};

C++11 template class Usage experience

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.