Go to main content
Oracle® Developer Studio 12.5: Debugging a Program with dbx

Exit Print View

Updated: June 2016

Debugging With C++ Templates

dbx supports C++ templates. You can load programs containing class and function templates into dbx and invoke any of the dbx commands on a template that you would use on a class or function:

Setting breakpoints at class or function template instantiations
Printing a list of all class and function template instantiations
Displaying the definitions of templates and instances
Calling member template functions and function template instantiations
Printing values of function template instantiations
Displaying the source code for function template instantiations

Template Example

The following code example shows the class template Array and its instantiations and the function template square and its instantiations.

 1        template<class C> void square(C num, C *result)
 2        {
 3             *result = num * num;
 4        }
 6        template<class T> class Array
 7        {
 8        public:
 9              int getlength(void)
10             {
11                    return length;
12              }
14              T & operator[](int i)
15              {
16                    return array[i];
17             }
19              Array(int l)
20              {
21                    length = l;
22                    array = new T[length];
23              }
25              ~Array(void)
26              {
27                    delete [] array;
28              }
30        private:
31              int length;
32              T *array;
33        };
35        int main(void)
36        {
37              int i, j = 3;
38             square(j, &i);
40             double d, e = 4.1;
41              square(e, &d);
43              Array<int> iarray(5);
44              for (i = 0; i < iarray.getlength(); ++i)
45              {
46                   iarray[i] = i;
47             }
49              Array<double> darray(5);
50              for (i = 0; i < darray.getlength(); ++i)
51             {
52                    darray[i] = i * 2.1;
53              }
55              return 0;
56        }

    In the example:

  • Array is a class template

  • square is a function template

  • Array<int> is a class template instantiation (template class)

  • Array<int>::getlength is a member function of a template class

  • square(int, int*) and square(double, double*) are function template instantiations (template functions)

Commands for C++ Templates

Use these commands on templates and template instantiations. Once you know the class or type definitions, you can print values, display source listings, or set breakpoints.

whereis Command

Use the whereis command to print a list of all occurrences of function or class instantiations for a function or class template.

For a class template:

(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

For a function template:

(dbx) whereis square
function template instance: `square<int>(__type_0,__type_0*)
function template instance: `square<double>(__type_0,__type_0*)
function template: `a.out`template_doc_2.cc`square

The __type_0 parameter refers to the 0th template parameter. A __type_1 would refer to the next template parameter.

For more information, see whereis Command.

whatis Command

Use the whatis command to print the definitions of function and class templates and instantiated functions and classes.

For a class template:

(dbx) whatis -t Array
template<class T> class Array
To get the full template declaration, try `whatis -t Array<int>’;

For the class template’s constructors:

(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);

For a function template:

(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);

For a class template instantiation:

(dbx) whatis -t Array<double>
class Array<double> {
    int Array<double>::getlength()
    double &Array<double>::operator [](int i);
    Array<double>::Array<double>(int l);
    int length;
    double *array;

For a function template instantiation:

(dbx) whatis square(int, int*)
void square(int num, int *result);

For more information, see whatis Command.

stop inclass Command

To stop in all member functions of a te mplate class:

(dbx)stop inclass Array
(2) stop inclass Array

Use the stop inclass command to set breakpoints at all member functions of a particular template class:

(dbx) stop inclass Array<int>
(2) stop inclass Array<int>

For more information, see stop in Command and inclass Event Specification.

stop infunction Command

Use the stop infunction command to set breakpoints at all instances of the specified function template:

(dbx) stop infunction square
(9) stop infunction square

For more information, see stop infunction Command and infunction Event Specification.

stop in Command

Use the stop in command to set a breakpoint at a member function of a template class or at a template function.

For a member of a class template instantiation:

(dbx) stop in Array<int>::Array(int l)
(2) stop in Array<int>::Array(int)

For a function instantiation:

(dbx) stop in square(double, double*)
(6) stop in square<double>(__type_0,__type_0*)

For more information, stop in Command and in Event Specification.

call Command

Use the call command to explicitly call a function instantiation or a member function of a class template when you are stopped in scope. If dbx is unable to determine the correct instance, it displays a numbered list of instances from which you can choose.

(dbx) call square(j,&i)

For more information, see call Command.

print Expressions

Use the print command to evaluate a function instantiation or a member function of a class template.

(dbx) print iarray.getlength()
iarray.getlength() = 5

Use print to evaluate the this pointer.

(dbx) whatis this
class Array<int> *this;
(dbx) print *this
*this = {
    length = 5
    array   = 0x21608

For more information, see print Command.

list Expressions

Use the list command to print the source listing for the specified function instantiation.

(dbx) list square(int, int*)

For more information, see list Command.