带有模板的容器声明
Container declaration with template
本文关键字:声明 更新时间:2023-10-16
这两个语句的字面意思是什么?它们只是两个初始化还是其他什么?
template <class T, size_t N> class array;
template <class T, class Alloc = allocator<T>> class deque;
正如您给出的两个已知类,即array
&deque
,所以可以详细地告诉你这些语法的含义。这是因为我们知道它们是如何实现的。
第一种情况:语句告诉程序员需要两个输入来创建array
类型的对象。例如:-
array<int, 5> a; // creates an array of 5(ie N) integers(ie T)
我们可以评论这件事,因为我们知道array
是什么。类似地,对于deque
,我们可以有:-
deque<int, SomeAllocator> d; // creates a deque container of integers(ie T) having a custom allocator named SomeAllocator (which must be an allocator of integers)
因为你是一个新手,所以最好假设allocator
是另一个类,它将allocate
空间,create
对象,destroy
对象&最后CCD_ 10空间。
我们可以再次对上面的内容发表评论,因为我们知道deque
是什么。但是,如果你写这样的东西:-
template <class T, size_t N> class A;
template <class T, class Alloc = allocator<T>> class D;
然后,代码只意味着A
在<> parenthesis
中接受两个参数的两个声明:一个数据类型(意味着任何类型,如int
、double
或some class
等)&一个CCD_ 17型&CCD_ 18还采用两个参数:一个数据类型(同样是任何类型)&该数据类型的CCD_ 19。
它们为类声明(但不定义)两个模板。在第一种情况下,模板参数是任意类型(可能是数组元素的类型)和数字(可能是阵列元素的数量)。在第二种情况下,模板参数都是类型,但第二种类型有默认值(取决于第一种类型)。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 在函数内部的声明中初始化数组,并在外部使用它
- Visual Studio中的函数声明和函数定义问题
- c++类声明时,相同的例程,不同的成员变量类型
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别