编译期的类型测试
一、使用模板特化机制
如比较两个类型是否一致:
VC8及以后版本支持了type traits机制:
| add_const Class // Makes const type from type. add_cv Class // Makes const/volatile type from type. add_pointer Class // Makes pointer to type from type. add_reference Class // Makes reference to type from type. add_volatile Class // Makes volatile type from type. aligned_storage Class // Makes suitably aligned type. alignment_of Class // Gets alignment of type. common_type Class conditional Class decay Class enable_if Class extent Class // Gets an array dimension. has_nothrow_assign Class // Tests whether the type does not throw on assign. has_nothrow_constructor Class // Tests whether the type does not throw on default construction. has_nothrow_copy Class // Tests whether the type does not throw on copy construction. has_nothrow_copy_constructor Class has_nothrow_default_constructor Class has_trivial_assign Class // Tests whether the type has trivial assign. has_trivial_constructor Class // Tests whether the type has trivial default constructor. has_trivial_copy Class // Tests whether the type has trivial copy constructor. has_trivial_copy_constructor Class has_trivial_default_constructor Class has_trivial_destructor Class // Tests whether the type has trivial destructor. has_virtual_destructor Class // Tests whether the type has virtual destructor. is_abstract Class // Tests whether |

本文介绍了C++中在编译期进行类型测试的方法,包括利用模板特化机制和Traits Classes来处理不同类型,特别是在STL的advance函数中的应用。通过Traits classes获取迭代器类型信息,结合模板元编程,实现编译期的类型检查和高效执行。
最低0.47元/天 解锁文章
775

被折叠的 条评论
为什么被折叠?



