C + + STL __type_traits<t> interpretation

Source: Internet
Author: User

See "STL Source Code Analysis" Today, suddenly saw a noun called __type_traits<t> .

When the container is destoy in scope, its function accepts the first and last iterators, and if all the elements in the [First,last] range call the destructor, but the destructor of this type T is irrelevant, it loses efficiency. __type_trais<t> can determine whether the destructor of the type T is insignificant, if (__true_type), then do nothing, otherwise call its destructor.

STL's Destroy function prototype:

Step Explanation:

1. First container trigger destroy(Iterator , Iterator last)

2.value_type(first) Gets the type t*the container is currently stored, and then calls __destroy(Iterator First, Iterator last, T*)

3. In the __destroy function, using typedef typename __type_traits<t>::has_trivial_destructor Trivial_destructor, Determines whether the destructor of type T has an attribute and calls __destroy_aux, passing in the Trivial_destructor

( if type T is non-characteristic, it defines typedef __true_type Has_trivial_destructor, otherwise defines typedef __false_type HAS_TRIVIAL_ destructor)

4. If there is a feature, it will be called to __destroy_aux (Iterator, Iterator, __false_type), and the elements in the [first, last] range are then destructor-

5. If there is no feature, it will be called to __destroy_aux (Iterator, Iterator, __true_type) and do nothing

PS: More specific understanding can be seen http://blog.csdn.net/sanlongcai/article/details/1786647 this blog post is simply the concept of type trait, and its test code is easy to understand.

Here, by the way, I recorded the test code of the above blog changes test, for later aftertaste. :)

1 #ifndef Test_class_h2 #defineTest_class_h3 4#include <iostream>5 usingstd::cout;6 usingStd::endl;7 8 classTestClass9 {Ten  Public: One TestClass () A     { -cout <<"TestClass Constructor call"<<Endl; -data =New int(3); the     } -TestClass (Consttestclass&Test_class) -     { -cout <<"TestClass copy constructor call. Copy data:" +<< * (test_class.data) <<Endl; -data =New int; +*data = * (test_class.data) *2; A     } at~TestClass () -     { -cout <<"TestClass destructor call. Delete the data:"<< *data <<Endl; - delete data; -     } - Private: in     int*data; - }; to  + #endif
TestClass.h
1 //my_type_traits.h Start2 #ifndef My_type_traits_h3 #defineMy_type_traits_h4 5 //#include "test_class.h"6 7 structMy_true_type {8 };9 Ten structMy_false_type { One }; A  -Template <classT> - structmy_type_traits the { - typedef my_false_type Has_trivial_destructor; - }; -  +Template<>structmy_type_traits<int> - { + typedef my_true_type Has_trivial_destructor; A }; at  - //template<> struct my_type_traits<testclass> - //{ - //typedef my_true_type Has_trivial_destructor; - //}; //If you delete the comment on the TestClass, the TestClass will be defined as a class with no attributes for the destructor -  in #endif - //My_type_traits.h End
My_type_traits.h
1 //my_destruct.h Start2 #ifndef My_destruct_h3 #defineMy_destruct_h4#include <iostream>5 6#include"My_type_traits.h"7 8 usingstd::cout;9 usingStd::endl;Ten  OneTemplate <classT1,classT2> AInlinevoidMyconstruct (T1 *p,Constt2&value) - { -     New(p) T1 (value); the } -  -Template <classT> -InlinevoidMydestroy (T *p) + { -typedef typename My_type_traits<t>::has_trivial_destructor Trivial_destructor;//gets whether the type T has no attribute destruction here + _mydestroy (P, Trivial_destructor ()); A } at  -Template <classT> -Inlinevoid_mydestroy (T *p, My_true_type) - { -cout <<"Do the trivial destructor"<<Endl; - } in  -Template <classT> toInlinevoid_mydestroy (T *p, My_false_type) + { -cout <<"Do the real destructor"<<Endl; thep->~T (); * } $ Panax Notoginseng #endif - //My_destruct.h End
my_destruct.h
1#include"Test_class.h"2#include"my_destruct.h"3 4 intMainvoid)5 {6     {7TestClass *Test_class_buf;8 TestClass Test_class;9 TenTest_class_buf = (TestClass *) malloc (sizeof(TestClass)); One myconstruct (Test_class_buf, test_class); A Mydestroy (TEST_CLASS_BUF); - Free (TEST_CLASS_BUF); -     } the  -     { -         int*int_p; -Int_p =New int; + Mydestroy (int_p); - Free (int_p); +     } A}
main.cpp

The figure is the result of a statement annotated with TestClass Has_trivial_destructor defined as __true_type in My_type_traits.h

Is the result of an annotation that is not annotated

C + + STL __type_traits<t> interpretation

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.