如何知道gcc选择了哪个函数重载

How to know which function overloading gcc selects

这是我的上下文:

#include <string>
#include <map>
#include <sstream>
#include <iostream>

namespace na {
    enum XXX { X1, X2 };

    std::string to_string(XXX x) {
        static const std::map<XXX, std::string> MAP { {X1, "X1"}, {X2, "X2"} };

        return MAP.at(x);
    }
}

namespace nb {
    enum YYY { Y1 = 1001, Y2 = 1002 };
}

typedef char priority;

std::string to_string(priority p) {
    std::ostringstream oss;
    oss << p;
    return oss.str();
}

int main()
{
    na::XXX x = na::X1;

    std::cout << "x: " << to_string(x) << "\n";

    nb::YYY y = nb::Y2;

    std::cout << "y: " << to_string(y) << "\n";

    return 0;
}

to_string(y) 的调用是 "mistake",因为类型 nb::YYY 不存在函数 to_string。但实际上这是可行的!

我用 :

编译它
g++ -o main.o -c -std=c++11 -Wall -Wextra -pedantic main.cpp
g++ -o function_overloading main.o

而且编译没有错误,没有警告。在那个简单的例子中,我知道当 GCC 为 to_string(y) 查找名称时,它会找到 to_string(priority) 但在一个大项目中,要找到 GCC 选择了哪个函数并不简单。

那么,有没有一种方法可以通过检查 .o 或将选项传递给 GCC 来确定 GCC 选择了哪个函数 "a posteriori"?

我在 linux 上使用 GCC 4.7.2。

您可能会使用源信息进行 objdump。让我们试试你的例子(为了清楚起见,我将使用 gcc 4.9.2:

g++ -c -O0 -gdwarf-2 gccover.cpp -std=c++11
objdump -Sdr gccover.o >& gccover.dis

您将看到:

<_ZN2na9to_stringENS_3XXXE>:
std::string to_string(XXX x)

<_Z9to_stringc>:
std::string to_string(priority p)

现在去呼叫点看看实际呼叫的是什么。

      std::cout << "y: " << to_string(y) << "\n";
 296: 8b 45 e8              mov    -0x18(%rbp),%eax
 299: 0f be d0              movsbl %al,%edx
 29c: 48 8d 45 e0           lea    -0x20(%rbp),%rax
 2a0: 89 d6                 mov    %edx,%esi
 2a2: 48 89 c7              mov    %rax,%rdi
 2a5: e8 00 00 00 00        callq  2aa <main+0x76>
      2a6: R_X86_64_PC32  _Z9to_stringc-0x4

我想,一切都清楚了。