模板内部类型定义 typename::int
来源:互联网 发布:kvm linux 编辑:程序博客网 时间:2024/06/07 05:36
Designating Types inside the Template Definition
在模板定义内部指定类型
In addition to defining data or function members, a class may define type members. For example, the library container classes define various types, such as size_type, that allow us to use the containers in a machine-independent way. When we want to use such types inside a function template, we must tell the compiler that the name we are using refers to a type. We must be explicit because the compiler (and a reader of our program) cannot tell by inspection when a name defined by a type parameter is a type or a value. As an example, consider the following function:
除了定义数据成员或函数成员之外,类还可以定义类型成员。例如,标准库的容器类定义了不同的类型,如 size_type,使我们能够以独立于机器的方式使用容器。如果要在函数模板内部使用这样的类型,必须告诉编译器我们正在使用的名字指的是一个类型。必须显式地这样做,因为编译器(以及程序的读者)不能通过检查得知,由类型形参定义的名字何时是一个类型何时是一个值。例如,考虑下面的函数:
template <class Parm, class U> Parm fcn(Parm* array, U value) { Parm::size_type * p; // If Parm::size_type is a type, then a declaration // If Parm::size_type is an object, then multiplication }
We know that size_type must be a member of the type bound to Parm, but we do not know whether size_type is the name of a type or a data member. By default, the compiler assumes that such names name data members, not types.
我们知道 size_type 必定是绑定到 Parm 的那个类型的成员,但我们不知道 size_type 是一个类型成员的名字还是一个数据成员的名字,默认情况下,编译器假定这样的名字指定数据成员,而不是类型。
If we want the compiler to treat size_type as a type, then we must explicitly tell the compiler to do so:
如果希望编译器将 size_type 当作类型,则必须显式告诉编译器这样做:
template <class Parm, class U> Parm fcn(Parm* array, U value) { typename Parm::size_type * p; // ok: declares p to be a pointer }
We tell the compiler to treat a member as a type by prefixing uses of the member name with the keyword typename. By writing typename Parm::size_type we say that member size_type of the type bound to Parm is the name of a type. Of course, this declaration puts an obligation on the types used to instantiate fcn: Those types must have a member named size_type that is a type.
通过在成员名前加上关键字 typename 作为前缀,可以告诉编译器将成员当作类型。通过编写 typename parm::size_type,指出绑定到 Parm 的类型的 size_type 成员是类型的名字。当然,这一声明给用实例化 fcn 的类型增加了一个职责:那些类型必须具有名为 size_type 的成员,而且该成员是一个类型。
If there is any doubt as to whether typename is necessary to indicate that a name is a type, it is a good idea to specify it. There is no harm in specifying typename before a type, so if the typename was unnecessary, it won't matter.
如果拿不准是否需要以 typename 指明一个名字是一个类型,那么指定它是个好主意。在类型之前指定 typename 没有害处,因此,即使 typename 是不必要的,也没有关系。
---------c++ primer
- 模板内部类型定义 typename::int
- 在模板定义内部指定类型
- 用模板template<typename T>实现int double等类型转string
- class 与 typename (在模板定义上)的通用与各自特殊性(前者-类,后者-嵌套依赖类型名)
- 定义模板时typename和class的区别
- 关于模板中的嵌套依赖类型名与typename
- 类模板参数、类型traits与typename关键字
- C++ primer 模版学习笔记-在模板定义内部指定类型
- python 内部如何实现int类型
- handle模板一 - 类型定义
- 模板 关键词 typename
- template<typename T,int n>
- 在模板定义语法中关键字class与typename的作用完全一样。
- int类型定义,float、double类型引用造成的结果
- c++ string与int(其他的内部类型)转换
- 模板中typename的作用
- 模板类 的 typename 关键字
- 模板编程中的typename作用
- extjs 中关于列表框(multiselect)的动态添加列表项的方法
- 推挽输出与开漏输出的区别
- 对CRITICAL_SECTION理解的总结
- 【转】智能设备项目:将程序生成CAB文件 并将CAB部署到PocketPC等仿真设备
- C#操作 Excel 详解
- 模板内部类型定义 typename::int
- java调用c++动态库
- InitializeCriticalSectionAndSpinCount()
- 表的连接
- Oracle 中的exception——(Raise)
- 收集SQLite中的时间日期函数
- Sql 递归查询
- [C++对象模型][2]指针与引用
- SQL研究-相似的数据类型