C++编译期的类型测试

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编译期的类型测试

一、使用模板特化机制

如比较两个类型是否一致:

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 the type is abstract class.

is_arithmetic Class        // Tests whether the type is arithmetic.

is_array Class            

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值