📄 types.cpp
字号:
/* The following code example is taken from the book * "C++ Templates - The Complete Guide" * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002 * * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */#include "typet.hpp"#include <iostream>class MyClass {};void myfunc(){}enum E { e1 };// check by passing type as template argumenttemplate <typename T>void check(){ if (TypeT<T>::IsFundaT) { std::cout << " IsFundaT "; } if (TypeT<T>::IsPtrT) { std::cout << " IsPtrT "; } if (TypeT<T>::IsRefT) { std::cout << " IsRefT "; } if (TypeT<T>::IsArrayT) { std::cout << " IsArrayT "; } if (TypeT<T>::IsFuncT) { std::cout << " IsFuncT "; } if (TypeT<T>::IsPtrMemT) { std::cout << " IsPtrMemT "; } if (TypeT<T>::IsEnumT) { std::cout << " IsEnumT "; } if (TypeT<T>::IsClassT) { std::cout << " IsClassT "; } std::cout << std::endl;}// check by passing type as function call argumenttemplate <typename T>void checkT (T){ check<T>(); // for pointer types check type of what they refer to if (TypeT<T>::IsPtrT || TypeT<T>::IsPtrMemT) { check<typename CompoundT<T>::BaseT>(); }}int main(){ std::cout << "int:" << std::endl; check<int>(); std::cout << "int&:" << std::endl; check<int&>(); std::cout << "char[42]:" << std::endl; check<char[42]>(); std::cout << "MyClass:" << std::endl; check<MyClass>(); std::cout << "ptr to enum:" << std::endl; E* ptr = 0; checkT(ptr); std::cout << "42:" << std::endl; checkT(42); std::cout << "myfunc():" << std::endl; checkT(myfunc); std::cout << "memptr to array:" << std::endl; char (MyClass::* memptr) [] = 0; checkT(memptr);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -