This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
std::string<ThrowOnCopy> behavior
- From: Ivan Baravy <i dot baravy at samsung dot com>
- To: libstdc++ at gcc dot gnu dot org
- Date: Fri, 14 Apr 2017 16:07:29 +0300
- Subject: std::string<ThrowOnCopy> behavior
- Authentication-results: sourceware.org; auth=none
- Cms-type: 201P
- References: <CGME20170414130729eucas1p1ad084f642d2a634d4137c3b89c9d5d8c@eucas1p1.samsung.com>
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);
}