C++奇怪的 ::template

答疑解惑 怎么会有::template的写法

起初

在阅读stl的源码的时候,发现了一条诡异的代码

// ALIAS TEMPLATE _Rebind_alloc_t
template<class _Alloc,class _Value_type>
using _Rebind_alloc_t = typename allocator_traits<_Alloc>::template rebind_alloc<_Value_type>;

在文件xmemory0中913行 我用的vs2017
两个冒号后面接了一个template,顿时我就懵逼了,这是什么意思?

分析

没记错的话
:: 两个冒号意思是前面一个作用域 类或者命名空间之类
template是一个关键字,一般用法都是写类或者函数前面,没有这样子用的呀,百思不解。
搜了一圈网上,几乎没有提这个东西的,倒是有一个博客有这样写,不过收费,而且还没看到有说的希望 博客连接

追踪

那就只能硬着头皮魔改猜测了,别看只有一行代码,挺复杂的,这句代码的意思是

给 allocator_traits<_Alloc> 的内嵌模板类rebind_alloc<_Value_type>起了一个别名叫_Rebind_alloc_t ,这里面提到三个类型,这三个东西挺恶心,都是模板类,基础模板类allocator_traits<_Alloc>又是一通继承搞的复杂的要命

allocator_traits<_Alloc>继承conditional_t
template<class _Alloc>
struct allocator_traits: conditional_t<_Is_default_allocator<_Alloc>::value,
			_Default_allocator_traits<_Alloc>, _Normal_allocator_traits<_Alloc>>
	{	// defines traits for allocators
	};
conditional_t是conditional类内类型type 的别名
template<bool _Test,
	class _Ty1,
	class _Ty2>
	using conditional_t = typename conditional<_Test, _Ty1, _Ty2>::type;
type是类型参数_Ty2的别名

真是新技能get,一般都是动态换子类的,这里tm的是动态换父类。

template<bool _Test,
	class _Ty1,
	class _Ty2>
	struct conditional
	{	// type is _Ty2 for assumed !_Test
	using type = _Ty2;
	};

好吧,我们看_Ty2从哪儿蹦出来的,一通倒追,我们知道

_Ty2是_Default_allocator_traits<_Alloc>
template<class _Alloc>
	struct _Default_allocator_traits
	{	// traits for std::allocator
	using allocator_type = _Alloc;
	using value_type = typename _Alloc::value_type;

	using pointer = value_type *;
	using const_pointer = const value_type *;
	using void_pointer = void *;
	using const_void_pointer = const void *;

	using size_type = size_t;
	using difference_type = ptrdiff_t;

	using propagate_on_container_copy_assignment = false_type;
	using propagate_on_container_move_assignment = true_type;
	using propagate_on_container_swap = false_type;
	using is_always_equal = true_type;
	//罪魁祸首
	template<class _Other>
		using rebind_alloc = allocator<_Other>;

	template<class _Other>
		using rebind_traits = allocator_traits<allocator<_Other>>;

罪魁祸首rebind_alloc 是allocator<_Other>的别名

template<class _Other>
		using rebind_alloc = allocator<_Other>;

搞半天这个东西就是一个类的内嵌模板类,还能这样用。

测试程序

带着疑问写了一个小测试

class A
{
public:
	template<typename T>
	class H 
	{
	public:
		void OutPut()
		{
			cout << "我草~" << endl;
		}
	};
};

A::template H<typename int> pp;
A::H<typename int> qq;

int main()
{
	pp.OutPut();
    std::cout << "Hello World!\n";
	qq.OutPut();
}

程序结果

程序结果

程序运行挺好,但是就是觉得别扭,真是反人类,就算是用内嵌模板类我也是这样用

A::H<typename int> qq

虽然很少用内嵌模板类,

A::template H<typename int> pp;

这种写法对于我来说的确是少见。

总结

  1. 要是知道::template的写法,这篇东西屁用没有,要是不知道可以解惑一下子。
  2. 可以说翻代码挺有意思的,忍不住说这stl模板库的代码真是难看的要命,写的也是反人类,设计的也是各种诡异思路,只能说大神就是大神。
  • 12
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当当小螳螂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值