dbx 支持 C++ 模板。可将包含类和函数模板的程序装入 dbx 中,并对要用于类或函数的模板调用任何 dbx 命令,例如:
在类或函数模板实例中设置断点(请参见stop inclass classname 命令、stop infunction name 命令和stop in function 命令)
打印所有类和函数模板实例列表(请参见whereis name 命令)
显示模板和实例的定义(请参见whatis name 命令)
调用成员模板函数和函数模板实例(请参见call function_name( parameters) 命令)
打印函数模板实例值(print 表达式)
显示函数模板实例的源代码(请参见list 表达式)
以下代码示例中显示了类模板 Array 及其实例以及函数模板 square 及其实例。
1 template<class C> void square(C num, C *result) 2 { 3 *result = num * num; 4 } 5 6 template<class T> class Array 7 { 8 public: 9 int getlength(void) 10 { 11 return length; 12 } 13 14 T & operator[](int i) 15 { 16 return array[i]; 17 } 18 19 Array(int l) 20 { 21 length = l; 22 array = new T[length]; 23 } 24 25 ~Array(void) 26 { 27 delete [] array; 28 } 29 30 private: 31 int length; 32 T *array; 33 }; 34 35 int main(void) 36 { 37 int i, j = 3; 38 square(j, &i); 39 40 double d, e = 4.1; 41 square(e, &d); 42 43 Array<int> iarray(5); 44 for (i = 0; i < iarray.getlength(); ++i) 45 { 46 iarray[i] = i; 47 } 48 49 Array<double> darray(5); 50 for (i = 0; i < darray.getlength(); ++i) 51 { 52 darray[i] = i * 2.1; 53 } 54 55 return 0; 56 } |
该示例中:
Array 是一个类模板
square 是一个函数模板
Array<int> 是类模板实例(模板类)
Array<int>::getlength 是模板类的成员函数
square(int, int*) 和 square(double, double*) 是函数模板实例(模板函数)
在模板和模板实例中使用这些命令。知道类或类型定义后,便可以打印值、显示源码列表或设置断点。
使用 whereis 命令可打印函数模板或类模板的函数或类实例的所有具体值列表。
对于类模板:
(dbx) whereis Array member function: `Array<int>::Array(int) member function: `Array<double>::Array(int) class template instance: `Array<int> class template instance: `Array<double> class template: `a.out`template_doc_2.cc`Array |
对于函数模板:
(dbx) whereis square function template instance: `square<int>(__type_0,__type_0*) function template instance: `square<double>(__type_0,__type_0*) |
__type_0 参数引用 0 号模板参数。__type_1 引用下一个模板参数。
有关更多信息,请参见whereis 命令。
使用 whatis 命令可打印函数模板和类模板的定义以及实例化的函数和类。
对于类模板:
(dbx) whatis -t Array template<class T> class Array To get the full template declaration, try `whatis -t Array<int>’; |
对于类模板的构造函数:
(dbx) whatis Array More than one identifier ’Array’. Select one of the following: 0) Cancel 1) Array<int>::Array(int) 2) Array<double>::Array(int> > 1 Array<int>::Array(int 1); |
对于函数模板:
(dbx) whatis square More than one identifier ’square’. Select one of the following: 0) Cancel 1) square<int(__type_0,__type_0*) 2) square<double>(__type_0,__type_0*) > 2 void square<double>(double num, double *result); |
对于类模板实例化:
(dbx) whatis -t Array<double> class Array<double>; { public: int Array<double>::getlength() double &Array<double>::operator [](int i); Array<double>::Array<double>(int l); Array<double>::~Array<double>(); private: int length; double *array; }; |
对于函数模板实例化:
(dbx) whatis square(int, int*) void square(int num, int *result); |
有关更多信息,请参见whatis 命令。
(dbx)stop inclass Array (2) stop inclass Array |
可使用 stop inclass 命令在特定模板类的所有成员函数中设置断点:
(dbx) stop inclass Array<int> (2) stop inclass Array<int> |
有关更多信息,请参见stop 命令和inclass classname [-recurse | -norecurse]。
可使用 stop infunction 命令在指定函数模板的所有实例中设置断点
(dbx) stop infunction square (9) stop infunction square |
有关更多信息,请参见stop 命令和infunction function。
可使用 stop in 命令在模板类的成员函数或在模板函数中设置断点。
对于类模板实例化的成员:
(dbx) stop in Array<int>::Array(int l) (2) stop in Array<int>::Array(int) |
对于函数实例化:
(dbx) stop in square(double, double*) (6) stop in square(double, double*) |
有关更多信息,请参见stop 命令和in function。
可使用 call 命令在作用域中停止时显式调用函数实例或类模板的成员函数。如果 dbx 无法确定正确的实例,它会显示一个带编号的实例列表,您可以从列表中进行选择。
(dbx) call square(j,&i) |
有关更多信息,请参见call 命令。
(dbx) print iarray.getlength() iarray.getlength() = 5 |
使用 print 对 this 指针求值。
(dbx) whatis this class Array<int> *this; (dbx) print *this *this = { length = 5 array = 0x21608 } |
有关更多信息,请参见print 命令。
(dbx) list square(int, int*) |
有关更多信息,请参见list 命令。