template template parameter模板模板参数笔记----C++学习之路
2017-11-12 02:06
656 查看
1.如标题所说,也就是在模板中嵌入另一个模板举个例子:
template<typename T,
template <typename T>
class Container
>
class XCls
{
private:
Container<T> c;
public:
....
};那么怎么取使用呢?
template<typename T>
using Lst = list<T,allocator<T>>;
因为在真正用得时候,这个list也就是Container要拿去list<string> c;这样看是可以得,但是容器有第二模板参数。
在语法上面是过不去得。
要解决这种问题得话,就使用:
template<typename T>
using Lst = list<T,allocator<T>>; 2.如上所说,因为容器可能会有第二第三模板参数,那么假如我们传入得是一个智能指针?
template<typename T,
template<typename T>
class SmartPtr
>
class XCls
{
private:
SmartPtr<T> sp;
public:
XCls() :sp(new T) { }
};那么使用:
XCls<string,shared_ptr> p1;
XCls<double,unique_ptr> p2; X
XCls<int,weak_ptr> p3; X
XCls<long,auto_ptr> p4;其中有两个错误得不是因为上一个得问题,是因为这两个智能指针本身得问题。
3.看起来像但是又是模板模板参数:
template<class T,class Sequence = deque<T>>
class stack
{
friend bool operator==<>(const stack&,const stack&);
frend bool operator< <>(const stack&,const stack&);
protected:
Sequence c;
....
};当这么写得时候:
stack<int> s1;这个第二参数是一个默认值。
stack<int,list<int>> s2;如果要写出,也要这样写,但是比较起来会发现,这个list得模板参数已经写死了,必须绑定出来。
所以这不是一个模板模板参数。
template<typename T,
template <typename T>
class Container
>
class XCls
{
private:
Container<T> c;
public:
....
};那么怎么取使用呢?
template<typename T>
using Lst = list<T,allocator<T>>;
XCls<string,list> mylst1; X XCls<string,Lst> mylst2;这里得第二段第一行,mylst1得意思就是,传进去得参数是个list模板,但是这一句是错误得,
因为在真正用得时候,这个list也就是Container要拿去list<string> c;这样看是可以得,但是容器有第二模板参数。
在语法上面是过不去得。
要解决这种问题得话,就使用:
template<typename T>
using Lst = list<T,allocator<T>>; 2.如上所说,因为容器可能会有第二第三模板参数,那么假如我们传入得是一个智能指针?
template<typename T,
template<typename T>
class SmartPtr
>
class XCls
{
private:
SmartPtr<T> sp;
public:
XCls() :sp(new T) { }
};那么使用:
XCls<string,shared_ptr> p1;
XCls<double,unique_ptr> p2; X
XCls<int,weak_ptr> p3; X
XCls<long,auto_ptr> p4;其中有两个错误得不是因为上一个得问题,是因为这两个智能指针本身得问题。
3.看起来像但是又是模板模板参数:
template<class T,class Sequence = deque<T>>
class stack
{
friend bool operator==<>(const stack&,const stack&);
frend bool operator< <>(const stack&,const stack&);
protected:
Sequence c;
....
};当这么写得时候:
stack<int> s1;这个第二参数是一个默认值。
stack<int,list<int>> s2;如果要写出,也要这样写,但是比较起来会发现,这个list得模板参数已经写死了,必须绑定出来。
所以这不是一个模板模板参数。
相关文章推荐
- member template成员模板笔记----C++学习之路
- variadic template模板参数可变化----C++学习之路
- c++无类型参数模板(non-type template parameter)
- <C++学习笔记>函数模板 template
- c++ template 学习之一:模板参数
- c++ template笔记(3)非类型模板参数nontype template parameters
- c++ template笔记(3)非类型模板参数nontype template parameters
- C++学习笔记36 (模板的细节明确template specialization)和显式实例(template instantiation)
- String类的设计笔记----C++学习之路
- 设计模式学习笔记---模板方法模式template method pattern(Java版)
- 强大的DataGrid组件[10]_自定义脚模板(FooterTemplate)——Silverlight学习笔记[18]
- C++学习笔记 - main()函数及其参数
- C++ Template学习笔记之函数模板(2)——函数模板实例化
- C++ Template学习笔记之函数模板(6)——模板显式特化
- c++学习笔记(模板)(一)
- 嵌入式开发之C++基础学习笔记5--静态成员,友元,运算符重载,模板,文件流
- C++ Standard Stl -- SGI STL源码学习笔记(03) STL中的模板编译期检查与偏特化编译期检查
- Template template parameter(模板参数) example
- 步步为营 .NET 设计模式学习笔记 十八、Template(模板模式)
- C++学习笔记之---参数特性