5

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.

c-urchin
  • 4,344
  • 6
  • 28
  • 30
  • http://stackoverflow.com/questions/1496497/how-can-i-see-parse-tree-intermediate-code-optimization-code-and-assembly-code might be useful -- ie, look at intermediate compiler steps. If you only want to do this once, you could look at the object files generated, and maybe stick a "marker" function call surrounding the call you want to understand. The object file will indicate what the symbol name of the function it called, which can be revered back into the function signature (which should be unique) that you called. – Yakk - Adam Nevraumont Nov 01 '12 at 17:25
  • 5
    I've often said that what we need is a compile-time debugger so that you can step through template instantiations as they're being evaluated, looking at template arguments, etc., to see what's really going on. – Pete Becker Nov 01 '12 at 17:27
  • When you say *see*, what do you mean exactly ? Clang is notorious for producing a fully formed AST for C++, however you would need to write a program around its libraries to be able to "see". – Matthieu M. Nov 01 '12 at 17:28
  • 1
    What problem are you trying to solve? – X-Istence Nov 01 '12 at 17:46
  • @PeteBecker: do you know if anyone is doing any work along those lines? That would be an incredible tool. – Michael Burr Nov 01 '12 at 21:01
  • @MichaelBurr - no, I don't know of anyone doing that sort of thing. – Pete Becker Nov 01 '12 at 23:08
  • @MichaelBurr: g++ is OS, go implement it! :D – Ed S. Nov 02 '12 at 03:44
  • @Matthieu M: I do mean "see" as in human-readable. So Clang plus a program to interpret the output would apparently do the trick. – c-urchin Nov 02 '12 at 18:59
  • @Yakk: that sounds like the simplest suggestion so far. Thanks. – c-urchin Nov 02 '12 at 19:02

2 Answers2

1

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.

Community
  • 1
  • 1
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • Thanks. If Eclipse can really do that I'm impressed and that would be a nice solution. – c-urchin Nov 02 '12 at 18:55
  • @c-urchin - Getting Eclipse to correctly recognize the various source and header files can be tricky, and particularly complex C++ code can rarely confuse it, but it's very nice once it's working. – Josh Kelley Nov 02 '12 at 19:03
  • @c-urchin - MS VS can do it too. Move mouse cursor to name of function and you can see name of calling overload function and number of overload functions. I think any modern IDE must do it. – SergV Nov 03 '12 at 08:15
1

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]
Community
  • 1
  • 1
SergV
  • 1,269
  • 8
  • 20
  • Thanks, but as I said I can't assume that I can modify the source code or that I even know what code might be a candidate since it might come from any #include'd file (at any level) – c-urchin Nov 02 '12 at 18:54