This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

std::string<ThrowOnCopy> behavior


Hello,

I attached an example where I expect an exception on the line 77, but
nothing happens. GDB reports that memmove is used and thus the
ThrowOnCopy objects are moved inside of a std::string and no exceptions
are thrown.

Is this behavior intentional?

Regards,
Ivan
#include <vector>
#include <cassert>
#include <string>

class ThrowOnCopy
{
public:
    ThrowOnCopy() : should_throw(false) {}
    explicit ThrowOnCopy(bool should_throw) : should_throw(should_throw) {}

    ThrowOnCopy(ThrowOnCopy const & other)
	: should_throw(other.should_throw)
    {
	if (should_throw)
	{
	    throw 0;
	}
    }

    ThrowOnCopy(ThrowOnCopy & other)
	: should_throw(other.should_throw)
    {
	if (should_throw)
	{
	    throw 0;
	}
    }

    ThrowOnCopy(ThrowOnCopy const && other)
	: should_throw(other.should_throw)
    {
	if (should_throw)
	{
	    throw 0;
	}
    }

    ThrowOnCopy(ThrowOnCopy && other)
	: should_throw(other.should_throw)
    {
	if (should_throw)
	{
	    throw 0;
	}
    }

    ThrowOnCopy& operator=(const ThrowOnCopy& t)
      {
	should_throw = t.should_throw;
	if (should_throw)
	{
	    throw 0;
	}
      }

    ThrowOnCopy& operator=(ThrowOnCopy& t)
      {
	should_throw = t.should_throw;
	if (should_throw)
	{
	    throw 0;
	}
      }

    bool should_throw;
};

int main()
{
  std::basic_string<ThrowOnCopy> s;
  s.insert(s.end(), 10, ThrowOnCopy());
  s.reserve(100);
  assert(s.size() == 10);
  s[6].should_throw = true;
  try
  {
    s.insert(s.cbegin(), 5, ThrowOnCopy());
    assert(0);
  }
  catch (int e)
  {
    assert(s.size() >= 10);
    return 0;
  }
  assert(0);
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]