【问题标题】:C++ std::string ConstructorC++ std::string 构造函数
【发布时间】:2008-10-09 22:58:08
【问题描述】:

对此的任何想法将不胜感激:

std::string s1 = "hello";
std::string s2 = std::string(s1);

我现在希望这两个字符串是独立的,即我可以将“, world”附加到 s2 并且 s1 仍然会读取“hello”。这是我在 windows 和 linux 上找到的,但是在 HP_UX 机器上运行代码似乎 s2 和 s1 是同一个字符串,所以修改 s2 会更改 s1。

这听起来很疯狂吗,有人见过类似的东西吗?

【问题讨论】:

  • 你能发布一个很短的复制程序吗?我很难相信一些基本的东西会被破坏。
  • 作为一个挑剔的选择,你的第二行可能只是 std::string s2(s1);没有理由创建额外的临时文件。

标签: c++ hp-ux stdstring


【解决方案1】:

虽然我无法重现 OP 的确切错误,但我在 HP-UX aCC 编译器中遇到了类似的错误。我在HP boards 上发布了它,最终得到了惠普的回复。基本上,他们的 aCC 版本 3.xx(3.70、3.73、3.67 等)搞砸了 std::string 的构造。我们不得不迁移到编译器的 6.xx 版本。我们当时遇到的问题是,没有可用于 PA-RISC 机器的 6.xx 编译器,只有 Itanium。我相信 2007 年 9 月发布了针对 PA-RISC 的 6.xx 编译器。

出现问题的代码是:

#include <iostream>
#include <string>

class S : public std::string  // An extension of std::string
{
public:
  explicit S(const char* s)
    : std::string(s)
  {
  }
};

class N     // Wraps an int
{
public:
  explicit N(int n)
    : _n(n)
  {}
  operator S() const   // Converts to a string extension
  {
    return _n == 0 ? S("zero") : (_n == 1 ? S("one") : S("other"));
  }
private:
  int _n;
};

int main(int, char**)
{
  N n0 = N(0);
  N n1 = N(1);

  std::string zero = n0;
  std::cout << "zero = " << zero << std::endl;
  std::string one = n1;
  std::cout << "zero = " << zero
            << ", one = " << one << std::endl;

  return 0;
}

这是打印的:
零 = 零
零 = 一,一 = 一

换句话说,从 n1 构造字符串 1 完全破坏了另一个字符串(字符串 0)。

注意事项:
要查看编​​译器的版本,请键入“aCC -V”
要查看机器类型,请输入“uname -m”(9000/800 ==> PA-RISC,ia64 ==> Itanium)

【讨论】:

    【解决方案2】:

    这一定是一个错误。 std::string 可以将引用计数字符串作为其实现,但是一旦它被更改,它应该“分叉”字符串。

    【讨论】:

      【解决方案3】:

      这对我来说确实是个错误。可以访问 HP/UX 的其他任何人都可以复制吗?

      你是说这个程序在两行显示相同的文本?

      #include <stdio.h>
      #include <string>
      
      int main () 
      {
          std::string s1 = "hello"; 
          std::string s2 = std::string(s1);   // note: std::string s2( s1); would reduce the number of copy ctor calls
      
          s2.append( ", world");
      
          printf( "%s\n", s1.c_str());
          printf( "%s\n", s2.c_str());
      }
      

      【讨论】:

      • 我尝试使用 HP 的 aCC 编译器 3.67 版在 HP-UX 上重现此内容。它工作正常。但是,我遇到了 aCC 编译器的字符串构造的严重问题。我将创建一个单独的帖子。
      【解决方案4】:

      这两个字符串的变化实际上是不同的,还是您使用了其他比较(例如 c_str() 返回的地址)?

      字符串的一些实现在复制时不会复制整个字符串,以加快复制长字符串的速度。如果您尝试对其中任何一个进行更改,则实现应复制字符串并进行适当的更改(最好作为同一操作的一部分)。

      【讨论】:

        【解决方案5】:

        这肯定是个缺陷。此示例逐字逐句来自 C++ 标准:

        string s1("abc");
        string::iterator i = s1.begin();
        string s2 = s1;
        *i = ’a’;  // Must modify only s1
        

        【讨论】:

          猜你喜欢
          • 2015-04-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-27
          • 1970-01-01
          • 1970-01-01
          • 2011-04-27
          相关资源
          最近更新 更多