1

I have been using GDBs "call" functionality for last few weeks and it seems very useful Code like below

 void VectorPrint(const std::vector<int>& v)
    {
      std::cout << "start printing vector\n";
        for (int i = 0; i < v.size(); ++i) { 
            std::cout << v[i]; 
            if (i != v.size() - 1) 
                std::cout << "\n"; 
        } 
        //std::cout << "\n printing end \n";
    }

(gdb) call VectorPrint(any1DVectorhere) This executes the above function and prints the vector contents on console.

The issue I am facing is, I am unable to call the same function if it is templated, something like below. GDB does not recognize the templated functions

template <typename Traits>
    template <typename myVec>
    void MyClass<Traits>::VectorPrint(const std::vector<myVec>& v)
    {
      std::cout << "start printing vector\n";
        for (int i = 0; i < v.size(); ++i) { 
            std::cout << v[i]; 
            if (i != v.size() - 1) 
                std::cout << "\n"; 
        } 
        //std::cout << "\n printing end \n";
    }

Could anyone suggest how to get the call functionality working with GDB in this usecase ? Any help is highly appreciated.

Thanks a lot in advance !

jblixr
  • 1,345
  • 13
  • 34
CutePoison
  • 131
  • 1
  • 6
  • 1
    gdb or any debugger can call only the methods that already exist. If template instantiation has never occurred in your code for a particular type, then that call cannot happen. Make sure there exists such instantiation and show how you call the function in gdb. – jblixr May 08 '20 at 04:49

1 Answers1

1

First, notice that you have to use the template at least once in your code for that specific instantiation of the template to be created by the compiler. Second, even if you do that, the name won't be just VectorPrint. It will be something similar to VectorPrint<type>. Try pressing TAB after writing call VectorPr to see the available instances that gdb is seeing.

I have written a longer explanation about this in your other question.

darcamo
  • 3,294
  • 1
  • 16
  • 27