对于 std::stringstream ss<< static_cast<int>(data[0]) + ".dat"; '+' 调用哪些方法?

问题描述 投票:0回答:1

为什么这段代码可以编译并运行?

预期表达是

ss<<"0x" << std::hex << "B_CS2_0x" << static_cast<int>(data[0])  + ".dat";

但是我真的很好奇为什么

ss<<"0x" << std::hex << "B_CS2_0x" << static_cast<int>(data[0])  + ".dat";
可以编译并且可以运行?

#include<iostream>
#include<sstream>

int main()
{
  unsigned char data[5]={0x25};
  std::stringstream ss;
  ss<<"0x" << std::hex << "B_CS2_0x" << static_cast<int>(data[0])  + ".dat";

  std::cout << ss.str() << std::endl;
}
c++ c++11
1个回答
0
投票
#include<iostream>
#include<sstream>

int main()
{
  unsigned char data[5]={0x25};
  std::stringstream ss;
  ss <<"0x" << std::hex << "B_CS2_0x" ;
  auto a = static_cast<int>(data[0])  + ".dat";
  ss << a;
  std::cout << ss.str() << std::endl;
}

我转换了你的代码以查看 + 的程序集是什么。

        movzx   eax, BYTE PTR [rbp-69]
        movzx   eax, al
        add     rax, OFFSET FLAT:.LC2
        mov     QWORD PTR [rbp-24], rax

正如你所看到的,它是指针和int之间内置的+操作。 更具体地说,a 是

const char *

© www.soinside.com 2019 - 2024. All rights reserved.