Detecting nested types in C ++

Source: Internet
Author: User

Please read http://blog.csdn.net/pongba/archive/2004/08/24/82783.aspx first

Introduction: it is a common problem to determine whether a built-in type has been defined within a type and then call a specific or default function accordingly. the example mentioned above is that when the teacher Class and the student class have defined the identity type (person_tag, how to perform general operations on the Register operation for instances of classes that do not define person_tag.

I have provided my own answer to the above blog, which is simplified and can be compiled in non-standard compilers such as vc6.

# Include <stdio. h> <br/> // the following code can be compiled in most compilers (including vc6 ), I think the dilemma of C ++ is that it is difficult to write code compatible with different compilers <br/> // (to ensure compatibility, discard the special feature, member templates, or even some local features, you will find that all of them can be replaced by others. There are many such examples in qt4, haha) <br/> // interestingly, if the code is printed using STD: cout, it will cause an internal error in the vc6 compiler, I can't help it. Please settle the bill. <br/> // D:/VC/vc98/include/ostream (316): Fatal error c1001: internal compiler error <br/> // (compiler file 'f:/9782/vc98/P2/src/P2/eh1_t. C ', line 577) <br/> struct student_tag {}; <br/> struct teacher_tag {}; <br/> struct teacher {typedef teacher_tag person_tag ;}; <br/> struct student {typedef student_tag person_tag ;}; <br/> Struct worker {}; <br/> // The call level. Observe the root number of the underline. It is easy to understand. <br/> // step 6 (no underline) in fact, you can use macros without using function templates .... forget it. Take care of the emotions and scopes of most people. <br/> template <typename T> inline void register (T const & P) {register _ (p, null );} <br/> // Step 1 (an underline) register a template without a tag <br/> template <typename T> inline void register _ (T const & P ,...) {printf ("others, registered! /N ") ;}< br/> // in step 1 (an underline), the type of null is positively interpreted when a tag is registered. <Br/> // What is interesting is that the Register _ In the function body is changed to register _ (I mentioned a mistake at the beginning), and vc6 will not overflow the stack (Inline has an effect, but is the template recursive expansion level limited ?) <Br/> template <typename T> inline void register _ (T const & P, typename T: person_tag *) {register _ (p, (T: person_tag *) (null) ;}< br/> // step 3 (two underscores) register the <br/> template <typename T> inline void register _ (T const & P, teacher_tag *) {printf ("teachers, registered! /N ");} // registered instructor <br/> // Step 5 (two underscores) register the <br/> template <typename T> inline void register _ (T const & P, student_tag *) {printf/* STD :: cout <*/("students, registered! /N ");} // register a student <br/>

 

The above is an example of heavy-load resolution, which is very simple.

The following is an embedded type decision:

 

 Template <bool n> <br/> struct bool_struct <br/>{< br/> Enum {value = n };< br/> PRIVATE: <br/> char * sizer [(size_t) n) + 10]; // prevents arrays of 0 lengths <br/> }; </P> <p> template <typename T> <br/> bool_struct <false> get_tag (T *,...); </P> <p> template <typename T> <br/> bool_struct <true> get_tag (T *, typename T: person_tag * P = NULL ); </P> <p> template <typename T> <br/> struct has_person_tag <br/>{< br/> public: <br/> Enum {value = sizeof (get_tag (T *) null, null) = sizeof (bool_struct <true> )}; // value <br/> typedef bool_struct <value> has_person_tag_trait; // trait <br/> };

Trait can be used for function overloading.

 

Test code:

Int main () <br/>{< br/> Register (worker (); <br/> Register (teacher ()); <br/> Register (student (); <br/> printf ("has_person_tag <worker >:: value: % d/N", has_person_tag <worker> :: value); <br/> printf ("has_person_tag <teacher >:: value: % d/N", has_person_tag <teacher >:: value ); <br/> printf ("has_person_tag <student>: Value: % d/N", has_person_tag <student >:: value); <br/> return 0; <br/>}

Comments

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.