STL list部分代码

本文深入探讨了C++模板元编程中的pointer_traits结构体及其实现细节,包括如何定义任意指针的特性,如元素类型、差异数类型以及如何重新绑定到其他类型。此外,还介绍了_Get_rebind_alias和_Replace_first_parameter的使用,展示了模板元编程的强大能力。

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

 

template<class _Ptr,
	class _Ty>
	using _Rebind_pointer_t = typename pointer_traits<_Ptr>::template rebind<_Ty>;
template<class _Ty>
	struct pointer_traits
	{	// defines traits for arbitrary pointers
	using element_type = typename _Get_element_type<_Ty>::type;
	using pointer = _Ty;
	using difference_type = typename _Get_ptr_difference_type<_Ty>::type;

	template<class _Other>
		using rebind = typename _Get_rebind_alias<_Ty, _Other>::type;

	using _Reftype = conditional_t<is_void_v<element_type>,
		char&,
		add_lvalue_reference_t<element_type>>;

	_NODISCARD static pointer pointer_to(_Reftype _Val)
		{	// convert raw reference to pointer
		return (_Ty::pointer_to(_Val));
		}
	};
		// STRUCT TEMPLATE _Get_rebind_alias
template<class _Ty,
	class _Other,
	class = void>
	struct _Get_rebind_alias
	{	// provide fallback
	using type = typename _Replace_first_parameter<_Other, _Ty>::type;
	};
template<class _Newfirst,
	class _Ty>
	struct _Replace_first_parameter;

template<class _Newfirst,
	template<class, class...> class _Ty,
	class _First,
	class... _Rest>
	struct _Replace_first_parameter<_Newfirst, _Ty<_First, _Rest...>>
	{	// given _Ty<_First, _Rest...>, replace _First
	using type = _Ty<_Newfirst, _Rest...>;
	};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值