当您的同事只使用整数文字时,这些文字将被转换为char - 但是,如果整数的值太大,您会看到警告。被调用的操作符是
std::string operator+(const std::string &s, char c); // actually it's basic_string<> with some CharT...
或+= 变体;)
关于如何查找所有电话。您可以编译(没有内联)所有代码,objdump it,grep 用于所有运算符出现,并在过滤后的地址上使用 addr2line:
$ cat string-plus.cpp
#include <string>
int main()
{
std::string a = "moof ";
a += 192371;
}
$ g++ -g string-plus.cpp
string-plus.cpp: In function ‘int main()’:
string-plus.cpp:5: warning: overflow in implicit constant conversion
$ objdump -Cd a.out | \
grep 'call.*std::string::operator+=(char)@plt' | \
tr -d ' ' | \
cut -d: -f1 | \
xargs addr2line -Cfe string-plus
main
??:0
然而,这并没有给我行号......至少呼叫站点在那里;)
-C 开关可启用 C++ 名称解构。这也可以使用 binutls c++filt 手动完成。
有趣的是,对于string 和char,是定义了operator+,但只有在使用operator+= 时,整数文字才会转换为char,我必须为要使用的operator+ 传递一个char 文字(或值)。
要查找您的运营商的错位名称:
$ cat a.cpp
#include <string>
int main()
{
std::string a = "moof ";
a = a + char(1);
}
$ g++ a.cpp
$ objdump -t a.out | c++filt | grep operator+ | cut -d ' ' -f1
08048780
$ objdump -t a.out | grep 0804878
08048780 w F .text 00000067 _ZStplIcSt11char_traitsIcESaIcEESbIT_T0_T1_ERKS6_S3_
最后一个是您要搜索的名称 - 可用于 grep w/o name demangling。
我真的不知道更好的方法来做到这一点...:/