...attached is a test case I got from a fellow attendee of
my meeting for what at first blush looks like a COW bug in
stdcxx string. The expected output on line 4 is:

2. cc1: 'H'

Martin
#include <string>
#include <iostream>

int main()
{
  std::string s1("hello");
  std::string const& cs1 = s1;
  std::string const  cs2(s1);

  char const& cc1 = cs1[0];
  char const& cc2 = cs2[0];

  std::cout << "1: cs1[0]: '" << cs1[0] << "' / "
	    << static_cast<void const*>(&cs1[0]) << "\n";
  std::cout << "1: cc1:    '" << cc1    << "' / "
	    << static_cast<void const*>(&cc1)    << "\n";
  s1[0] = 'H';

  std::cout << "2: cs1[0]: '" << cs1[0] << "' / "
	    << static_cast<void const*>(&cs1[0]) << "\n";
  std::cout << "2: cc1:    '" << cc1    << "' / "
	    << static_cast<void const*>(&cc1)    << "\n";
  std::cout << "2: cc2:    '" << cc2    << "' / "
	    << static_cast<void const*>(&cc2)    << "\n";
}

Reply via email to