Debugging C++ Applications Using HP WDB (766162-001, March 2014)
Example 17 Sample Program for Debugging Template Class
1 template <class T>
2 class A {
3 public :
4 A(T arg) { data = arg; }
5 T foo();
6 private :
7 T data;
8 };
9
10 template <class T>
11 T A<T>::foo() {
12 return data++;
13 }
14
15 int main() {
16 A<int>a(10);
17 A<double> b(3.14);
18 int i;
19 double f;
20
21 i = a.foo();
22 f = b.foo();
23 return 0;
24 }
The WDB output snippet for this program is as shown below:
(gdb) b A::foo // Breakpoint on member function of template class.
[0] cancel
[1] all
[2] A<double>::foo() at template:12
[3] A<int>::foo()at template.C:12
> 2
Breakpoint 1 at 0x4000c00:2: file template.C, line 12 from/C++WhitePaper/template.
(gdb) ptype A // ptype of template
[1] A<int>
[2] A<double>
Enter your choice [1-2].
> 1
type = class A<int> {
private:
int data;
public:
void A(int);
int foo();
}
(gdb) call a.foo() // Member function call
$1 = 10
(gdb)
$2 = 11
Other commands, such as ptype, list, call, and so on, which are passed with function name
as argument also displays the menu for all instantiations.
When you have a typedef class as a template parameter, you can set a breakpoint on a member
function by using the following command:
break Class <typedef_classB>::memfunc
Example 18 shows a sample program for debugging template member function.
Debugging C++ templates 29