Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can compiler tell me which overloaded or template function it chose?

Specifically using g++ on linux, is there a way to determine which overloaded or template function was chosen for a particular statement?

More specifically, I don't assume that I necessarily know all the possible choices, which may be in header files coming from various libraries. And even if I did, I don't assume that I could modify the relevant code.

like image 583
c-urchin Avatar asked Nov 04 '22 12:11

c-urchin


2 Answers

I don't know of a way to do this directly.

The simplest solution is to set a breakpoint at the call site and single-step into whatever function is called; your debugger can then tell you which function you're in.

An IDE like Eclipse CDT can do overload and template resolution itself (if everything works right), so right-clicking on a function call and going to the function declaration will take you to the appropriate function.

By deliberately creating an ambiguous function call, as described in this answer, you can get a list of all available overloads and templates and can probably figure out which one is being invoked from there.

As Matthieu M. said, Clang can dump its AST. This requires some interpretation, but it can help you figure out which function is being called.

like image 194
Josh Kelley Avatar answered Nov 12 '22 09:11

Josh Kelley


Partial answer.

You can use non standard macro for printing name of function in run-time (Macro / keyword which can be used to print out method name?

For GNU C++:

#include <iostream>
using namespace std;
template <typename T>
void f(const T & t)
{
   cout << __PRETTY_FUNCTION__ << endl;
}
void f(const string &)
{
   cout << __PRETTY_FUNCTION__ << endl;
}
void f(int)
{
   cout << __PRETTY_FUNCTION__ << endl;
}
int main()
{
    f(1.0);
    f(1);
    f(string("sss"));
    string a;
    f(a);
}

Output from this code (http://ideone.com/PI39qK):

void f(int)
void f(int)
void f(const std::string&)
void f(T&) [with T = std::string]
like image 43
SergV Avatar answered Nov 12 '22 09:11

SergV