Skills in the new thinking of C ++ programming and new thinking of Programming

Source: Internet
Author: User
Tags traits

Skills in the new thinking of C ++ programming and new thinking of Programming

1. compiler assertions

The skills are roughly the same as those in the later sections. They all use the special features, but static_assert is already available in C ++ 0X, so I feel that this is useless, more just broaden your horizons 2. bitrate is a special process for a specific type. After all, the template will generate all operations of the same type, but sometimes we need to process specific types, as a result, we have made things special and biased. 3. the C ++ syntax that has been ignored by a local class can be defined directly in the class or function. However, it is correct to ignore it all the time. I think this is quite interesting. 4. constant ing is type: template <int v> struct Int2Type {enum {value = v ;}}. In this case, Int2Type is instantiated with different numbers each time, they are all different types of books.
 1 <class T,bool flag> 2 class myclass 3 { 4      void DoSomething () 5     { 6           if(flag ) 7          { 8               T *new_obj = p_old_obj-> clone(); 9          }10           else11          {12               T *new_obj = new T(p_olb_obj );13          }14     }15 };

 

The above program has two problems: 1. if the T type declares the copy constructor as private or protect, the compilation error is 2. if p_old_obj has nothing to do with clone, we can modify the following program in the case of a compilation error:
template <class T, bool isPolymorphic >class NiftyContainer{private:     void DoSomething (T * pobj,int2type <true>)    {          T *pNewObj = pobj-> clone();    }      void DoSomething (T * pobj,int2type <false>)    {          T *pNewObj = new T(pobj );    } public:     void DoSomething (T * pobj)    {         DoSomething(pobj ,int2type< isPolymorhpic>());    }};

 

In this way, as long as your program implements clone or sets the replication constructor to public, as long as one of the conditions is met, it can be compiled and passed, in addition, the reason for selecting the action to be compiled is that the clone or public copy constructor can be passed because the compiler does not compile a class member function that is not used. when we program type-to-type mappings, we often need to create a function to generate a new object.
template <class T, class U >T *create (const U &arg ){     return new T( arg);}

 

So what if we need to construct a widget object, but this object needs-1 as the construction parameter ?? We can use type2type to let the compiler choose which template function we want to use
template <class T, class U >T *create (const U &arg ,type2type< T>){     return new T( arg);} template <class U>widgets *create (const & arg,type2type <widgets>){     return new widgets( arg,-1);}

 

6. With the previous foundation for type selection, it is easy to see the next few items. The principle is similar to 3 (Type ing type), which is actually to use the function's eccentric mechanism.
template <bool flag, class T ,class U>struct select{     typedef T result;}; template <class T, class U >struct select <false, T,U >{     typedef U result;};

 

We can decide whether to use the T or U type during compilation based on the flag, but the example in this book is used to select whether to use a pointer or a common type, I think this example is not applicable to traits 7. during the compilation period, determine whether a type can be converted to another type (this is not accurate, or more accurately, whether the type is implicitly converted to another type)
template <class T, class U >class conver{     typedef char small;         class big    {          char dummy [2];    };         static small test( U);         static big test(...);         static T makeT();public:     enum    {          exists = sizeof ( test( makeT() ) ) == sizeof(small )    };};

 

In this way, when writing a program, we can directly determine whether a class can be converted into another class. I think this is an interesting template technique. in fact, both smalltest and bigtest and makeT are not instantiated, because sizeof does not have instantiation operations, so even if no function is defined, it is still compiled by 2. one more makeT function is declared because some classes will declare the constructor as private. If makeT is not used, T () 3 cannot be called. the template will find the best matching condition, that is, if we declare another type Z, put static big test (...) changed to static big test (Z). During program compilation, even if the two types can be converted to each other, such as int and size_t, the compiler will call test (Z ), in this way, we cannot see whether the two classes can be converted to each other. Otherwise, if test is (...) as declared in this way, the compiler will think that test (U) is more than test (...) match, so use the test (U) function 8. type_info has nothing to do with the override class. It is nothing more than that type_info cannot be copied, So I re-wrote a class package. defining Nulltype and emptyType replication classes is nothing to say 10. traits has mentioned http://www.cnblogs.com/linyilong3/p/3379433.html in the previous article. in general, to understand these skills, you need to understand the following points: 1. template specialization 2. the compiler finds the most appropriate matching option to match the type to be instantiated.

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.