C + + template determines whether the class type

Source: Internet
Author: User

/* The following code example is taken from the book* "C + + templates-the Complete Guide" * by David Vandevoorde and Nicol Ai M. Josuttis, Addison-wesley, 2002** (C) Copyright David Vandevoorde and Nicolai M. josuttis 2002.* Permission to copy, Use, modify, sell and distribute this software* are granted provided this copyright notice appears in all copies.* this SOF Tware is provided ' as is ' without express or implied* warranty, and with no claim as to it suitability for any purpose.*/  #include <iostream>template<typename t>class isclasst {private:typedef char one;typedef struct {char a[2];} Two;template<typename c> static one test (int C::*), template<typename c> static, test (...); public:enum {Yes = sizeof (test<t> (0)) = = 1};enum {No =! Yes};}; Class MyClass {};struct mystruct {};union myunion {};void myfunc () {}enum E {e1} e;//check by passing type as template a Rgumenttemplate <typename t>void Check () {if (Isclasst<t>::yes) {Std::cout << "isclasst" << Std::endl;} else {std::cout << "! Isclasst "<< Std::endl;}} Check by passing type as function call Argumenttemplate <typename t>void checkt (T) {check<t> ();} int main () {std::cout << "int:";check<int> (); Std::cout << "MyClass:";check<myclass> (); std::c Out << "MyStruct:"; MyStruct S;checkt (s); Std::cout << "myunion:";check<myunion> (); Std::cout << "enum:"; Checkt (e); std: : cout << "MyFunc ():"; Checkt (MyFunc);}

  

C + + template determines whether the class type

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.