1

关于语义和性能的几个问题:

x = 0;
While (x < 10) {
  std::cout << "Some text here to send to cout";
  ++x;
}

我使用 gcc 4.7,要流式传输的文本是否应该包含在 std::move 中?

像这样:

   x = 0;
    While (x < 10) {
      std::cout << std::move("Some text here to send to cout");
      ++x;
    }

虽然我在问,在这种情况下是否更好地将字符串设为静态,例如:

x = 0;
While (x < 10) {
  static const char* s = "Some text here to send to cout";
  std::cout << s;
  ++x;
}
4

2 回答 2

3

移动一个字符串字面量对你没有多大好处:它在任何情况下都会产生一个指针,并且这个指针将按值传递。关于使字符串文字静态化,我希望它根本没有区别。

于 2012-10-02T22:04:46.123 回答
1

没有也没有。operator<<它的参数是右值还是左值没有任何区别const char *:在后一种情况下,标准的左值到右值(纯概念)转换将在传递给之前自动应用operator<<

于 2012-10-02T22:06:34.553 回答