"move constructor for std::runtime_error" Code Answer


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:

  1. std::string : this is typically a short-string-optimized type with a copy constructor and copy assignment that is capable of throwing exceptions.

  2. std::runtime_error : this is (or holds) an immutable reference-counted string. this will never throw on copy construction or copy assignment.


explicit runtime_error(string&&);

can never (efficiently) transfer resources from the "type 1" string to the "type 2" string.

By ted.strauss on May 24 2022
Only authorized users can answer the Search term. Please sign in first, or register a free account.