• 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>;
    
    • 1
    • 2
    • 3

    在文件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
    	};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    conditional_t是conditional类内类型type 的别名
    template<bool _Test,
    	class _Ty1,
    	class _Ty2>
    	using conditional_t = typename conditional<_Test, _Ty1, _Ty2>::type;
    
    • 1
    • 2
    • 3
    • 4
    type是类型参数_Ty2的别名

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

    template<bool _Test,
    	class _Ty1,
    	class _Ty2>
    	struct conditional
    	{	// type is _Ty2 for assumed !_Test
    	using type = _Ty2;
    	};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    好吧,我们看_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>>;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

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

    template<class _Other>
    		using rebind_alloc = allocator<_Other>;
    
    • 1
    • 2

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

    测试程序

    带着疑问写了一个小测试

    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();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    程序结果

    程序结果

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

    A::H<typename int> qq
    
    • 1

    虽然很少用内嵌模板类,

    A::template H<typename int> pp;
    
    • 1

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

    总结

    1. 要是知道::template的写法,这篇东西屁用没有,要是不知道可以解惑一下子。
    2. 可以说翻代码挺有意思的,忍不住说这stl模板库的代码真是难看的要命,写的也是反人类,设计的也是各种诡异思路,只能说大神就是大神。
  • 相关阅读:
    测试总结的重要性
    矩阵分析与应用+张贤达
    Java 8 引进的一个新特性 Optional
    计算机毕业设计之流浪宠物管理系统
    react +antd-mobile图片上传插件(ImageUploader,支持七牛云直传)
    以太坊硬分叉愈演愈烈:为了分叉而分叉or保全矿工利益?
    介绍一下浏览器的缓存(Expires, Cache-Control等)
    R语言GARCH-DCC模型和DCC(MVT)建模估计
    Android教程之Android Compose 中实现类似链接反应弹出窗口的弹出窗口(教程含源码)
    Guava中常用Object方法-equals与null比较、hashCode、自定义toString、自定义compareTo排序
  • 原文地址:https://blog.csdn.net/shiyongfu19890308/article/details/136265034