为什么 std:string + 没有int 给出编译错误?

发布于 2024-10-09 01:34:57 字数 269 浏览 0 评论 0原文

我们刚刚发现我们的同事认为他可以向 std::string 添加一个整数,并在他的代码中到处使用了这样的操作。

我们没有看到此操作有任何编译器错误,我不明白为什么:没有 operator+ (const string& lhs, int rhs);。 int 会默默地转换为 char 吗? (使用 gcc Red Hat 4.1.2 和 -Wall 开关进行编译)。

而且,最重要的是,我们如何找到所有将 i​​nt 添加到 std::string 的行?

We just found our colleague thought he can add an integer to std::string and used such operation here and there in his code.

We don't see any compiler error for this operation and I don't understand why: there is no operator+ (const string& lhs, int rhs);. Is int silently casted to char? (Compiling with gcc Red Hat 4.1.2 and -Wall switch).

And, most important, how do we find all the lines where int added to std::string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人的歌 2024-10-16 01:34:57

当您的同事仅使用整数文字时,它们将被转换为 char - 但是,如果整数值太大,您将看到警告。被调用的运算符是

std::string operator+(const std::string &s, char c); // actually it's basic_string<> with some CharT...

或者 += 变体 ;)

关于如何查找所有调用。您可以编译(不内联)所有代码,objdump 它,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


有趣的是,stringcharoperator+ 定义,但仅限于使用 operator+ 时= 整数文字转换为 char,我必须传递 char 文字(或值)才能使用 operator+


查找操作员的损坏名称

$ 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 不进行名称损坏。

我真的不知道更好的方法来做到这一点......:/

When your colleague only used integer literals, these will be converted to char - You will however see a warning if the value of the integer is too big. The operator that gets called is

std::string operator+(const std::string &s, char c); // actually it's basic_string<> with some CharT...

Or the += variant ;)

On how to find all the calls. You could compile (w/o inlining) all the code, objdump it, grep for all the operator occurrences and use addr2line on the filtered addresses:

$ 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

This however hasn't given me the line number... At least the call site is there ;)

The -C switch enables c++ name demangling. Which can also be done manually with binutls c++filt.


Funnily enough, there is a definition of operator+ for string and char, but only when using operator+= the integer literal is converted to char, I have to pass a char literal (or value) for operator+ to be used.


To find the mangled name of your operator:

$ 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_

The last one is the name you are searching for - which can be used to grep w/o name demangling.

I really don't know a better way to do this... :/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文