alias template - using的使用方法

1.alias template

(1).template <typename T>
using vec = std::vector<T, MyAlloc<T>>;
vec<int> coll; //完美
(2).#define vec<T> template <typename T> std::vector<T, MyAlloc<T>>;
vec<int> coll;
template <typename int> std::vector<int, MyAlloc<int>> coll; //前缀template <typename int> 不是我们想要的。
(3).typedef std::vector<int, MyAlloc<int>> vec;
vec coll;  //typedef接收typename的参数

2.template template params

template<typename T, template <class> class Container>
class IndefCC {
	private:
		Container<T> c;
	public:
		IndefCC(){
			c.insert(c.end(), T()); //类型()是一个对象
		}
}
IndefCC<Mystring, vector> c1; //编译报错。vector有两个template arguments, 而Container仅有一个template argument。

解决的方法:
template template params + alias template
template <typename T>
using vec = std::vector<T, MyAlloc<T>>;
IndefCC<Mystring, vec> c1;  //完美
还可以是list和deque
template <typename T>
using lst= std::list<T, MyAlloc<T>>;
IndefCC<Mystring, lst> c1;
template <typename T>
using deq= std::deque<T, MyAlloc<T>>;
IndefCC<Mystring, deq> c1;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值