Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to `print`/evaluate c++ template functions in gdb

Tags:

c++

templates

gdb

I was wondering if it is possible to use gdb print command to evaluate results of c++ template functions. In the following code with a simple id function, I tried to print results of id(x), but it's as if id or id<t> never existed. The code I use is below, compiled with g++ -std=c++11 -g test7.cpp:

template<typename T>
T id(T x) {return x;}

int main() {
  int i = 0;
  i = i + 1;
} 

In GDB, I tried to print as follows:

Breakpoint 1, main () at test7.cpp:6
6         i = i + 1;
(gdb) print i
$1 = 0
(gdb) print id(i)
No symbol "id" in current context.
(gdb) print id<int>(i)
No symbol "id<int>" in current context.

As you can see, I always get "No symbol id".

There is a related post about GDB not allowing stepping into template functions in OSX. In the answers there, the template function can at least be disassembled. In my case, even disassemble gives nothing:

(gdb) disassemble id<int>
No symbol "id<int>" in current context.

Is it possible to evaluate template functions at all?

P.S. I am using GDB 7.6.1 coming from TDM-GCC (4.8.1-2).

Thanks.

like image 516
thor Avatar asked Jul 25 '14 13:07

thor


People also ask

What is the syntax of function template?

A function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.

How the instantiation of function template happens?

When a function template is first called for each type, the compiler creates an instantiation. Each instantiation is a version of the templated function specialized for the type. This instantiation will be called every time the function is used for the type.

How function template is defined?

Function templates are similar to class templates but define a family of functions. With function templates, you can specify a set of functions that are based on the same code but act on different types or classes.

What is the correct form of declaring a template in C++?

Class Template Declarationtemplate <class T> class className { private: T var; ... .. ... public: T functionName(T arg); ... .. ... }; In the above declaration, T is the template argument which is a placeholder for the data type used, and class is a keyword.


1 Answers

Without an explicit instance in the source code, the compiler will treat the template code as it would "static inline" code and optimize it out if it is unused. An explicit instance will create a symbol with external linkage (although it could still be technically optimized away by the linker, but in my test it did not...):

template<typename T>
T id(T x) {return x;}

template int id<int> (int x);

int main() {
  int i = 0;
  i = i + 1;
} 

Within gdb, I place the C++ function I want to call within single quotes:

Breakpoint 1, main () at tmpl.cc:7
7     int i = 0;
(gdb) n
8     i = i + 1;
(gdb) p i
$1 = 0
(gdb) p 'id<int>(int)'(i)
$2 = 0
(gdb)

Your question in your comment about creating an explicit instance of a variadic template, the syntax is the same. You have to create a different explicit instance for each different parameter list you plan to call the template with.

like image 149
jxh Avatar answered Sep 21 '22 16:09

jxh