Can you assign a substring of a std::string to itself?
No.
This operation is defined by [string::assign]/4:
basic_string& assign(const basic_string& str, size_type pos, size_type n = npos);
Effects: Determines the effective length
rlen
of the string to assign as the smaller ofn
andstr.size() - pos
and callsassign(str.data() + pos rlen)
.
(dat typo)
Then:
basic_string& assign(const charT* s, size_type n);
Effects: Replaces the string controlled by
*this
with a string of lengthn
whose elements are a copy of those pointed to bys
.
Nothing about this says anything about whether str.assign(str, 0)
is at all safe (in particular, we have no way of knowing when the copy of each character will occur!).
Therefore I strongly suggest you avoid doing it.