Why does std::runtime_error
not provide a constructor accepting an std::string&&
? Looking at the constructors for std::string
, it has a move constructor, but the noexcept
specification is only there for C++14, not C++11. Was this a mistake, a deadline that was missed or am I missing something?
相关问题
- how to split a list into a given number of sub-lis
- thread_local variables initialization
- how to call a C++ dll from C# windows application
- Generate string from integer with arbitrary base i
- Google Test - generate values for template class i
相关文章
- JSP String formatting Truncate
- Selecting only the first few characters in a strin
- std::function copying parameters?
- Python: print in two columns
- Is a compiler forced to reject invalid constexpr?
- JUnit continue to assert things after expected exc
- extending c++ string member functions
- Google app engine datastore string encoding proble
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.