"move constructor for std::runtime_error" Code Answer
1
explicit runtime_error(string&&);
does not exist simply because it would not provide any optimization.
as it turns out, a c++11-conforming runtime_error does not internally store a std::string. the reason is that the copy members of runtime_error must not throw exceptions. otherwise the wrong exception could get thrown when the compiler copies the exception object in the process of throwing it.
this implies that runtime_error needs to store a non-mutable reference counted string. however c++11 outlaws the cow-implementation for std::string. implementations of std::string have moved to a "short-string-optimization" which must allocate on copy construction if the length of the string is beyond the "short limit". and there is no limit on the length of strings used to construct a runtime_error.
so effectively c++11 (and forward) contains two implementations of strings:
std::string : this is typically a short-string-optimized type with a copy constructor and copy assignment that is capable of throwing exceptions.
std::runtime_error : this is (or holds) an immutable reference-counted string. this will never throw on copy construction or copy assignment.
and
explicit runtime_error(string&&);
can never (efficiently) transfer resources from the "type 1" string to the "type 2" string.
explicit runtime_error(string&&);
does not exist simply because it would not provide any optimization.
as it turns out, a c++11-conforming
runtime_error
does not internally store astd::string
. the reason is that the copy members ofruntime_error
must not throw exceptions. otherwise the wrong exception could get thrown when the compiler copies the exception object in the process of throwing it.this implies that
runtime_error
needs to store a non-mutable reference counted string. however c++11 outlaws the cow-implementation forstd::string
. implementations ofstd::string
have moved to a "short-string-optimization" which must allocate on copy construction if the length of the string is beyond the "short limit". and there is no limit on the length of strings used to construct aruntime_error
.so effectively c++11 (and forward) contains two implementations of strings:
std::string
: this is typically a short-string-optimized type with a copy constructor and copy assignment that is capable of throwing exceptions.std::runtime_error
: this is (or holds) an immutable reference-counted string. this will never throw on copy construction or copy assignment.and
explicit runtime_error(string&&);
can never (efficiently) transfer resources from the "type 1" string to the "type 2" string.