From: Markus Scherer <markus.icu@gmail.com> Date: Thu, 18 Jun 2015 12:19:20 +0000 (+0000) Subject: ICU-10119 better document aliasing vs. copy/move/RVO X-Git-Tag: milestone-59-0-1~1059 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7f8d3dfe07c99c9baa723123f2440c9b48e945aa;p=icu ICU-10119 better document aliasing vs. copy/move/RVO X-SVN-Rev: 37599 --- diff --git a/icu4c/source/common/unicode/unistr.h b/icu4c/source/common/unicode/unistr.h index dfcc8def2fc..b986269f638 100644 --- a/icu4c/source/common/unicode/unistr.h +++ b/icu4c/source/common/unicode/unistr.h @@ -215,6 +215,16 @@ class UnicodeStringAppendable; // unicode/appendable.h * similar functionality as the Java String and StringBuffer/StringBuilder classes. * It is a concrete implementation of the abstract class Replaceable (for transliteration). * + * A UnicodeString may also "alias" an external array of characters + * (that is, point to it, rather than own the array) + * whose lifetime must then at least match the lifetime of the aliasing object. + * This aliasing may be preserved when returning a UnicodeString by value, + * depending on the compiler and the function implementation, + * via Return Value Optimization (RVO) or the move assignment operator. + * (However, the copy assignment operator does not preserve aliasing.) + * For details see the description of storage models at the end of the class API docs + * and in the User Guide chapter linked from there. + * * The UnicodeString class is not suitable for subclassing. * * <p>For an overview of Unicode strings in C and C++ see the @@ -273,7 +283,7 @@ class UnicodeStringAppendable; // unicode/appendable.h * significant performance improvements. * Also, the internal buffer is accessible via special functions. * For details see the - * <a href="http://icu-project.org/userguide/strings.html">User Guide Strings chapter</a>.</p> + * <a href="http://userguide.icu-project.org/strings#TOC-Maximizing-Performance-with-the-UnicodeString-Storage-Model">User Guide Strings chapter</a>.</p> * * @see utf.h * @see CharacterIterator @@ -1847,9 +1857,20 @@ public: /** * Assignment operator. Replace the characters in this UnicodeString * with the characters from <TT>srcText</TT>. + * + * Starting with ICU 2.4, the assignment operator and the copy constructor + * allocate a new buffer and copy the buffer contents even for readonly aliases. + * By contrast, the fastCopyFrom() function implements the old, + * more efficient but less safe behavior + * of making this string also a readonly alias to the same buffer. + * + * If the source object has an "open" buffer from getBuffer(minCapacity), + * then the copy is an empty string. + * * @param srcText The text containing the characters to replace * @return a reference to this * @stable ICU 2.0 + * @see fastCopyFrom */ UnicodeString &operator=(const UnicodeString &srcText); @@ -1871,6 +1892,9 @@ public: * including its contents, for example for strings from resource bundles * or aliases to string constants. * + * If the source object has an "open" buffer from getBuffer(minCapacity), + * then the copy is an empty string. + * * @param src The text containing the characters to replace. * @return a reference to this * @stable ICU 2.4 @@ -3171,8 +3195,19 @@ public: /** * Copy constructor. + * + * Starting with ICU 2.4, the assignment operator and the copy constructor + * allocate a new buffer and copy the buffer contents even for readonly aliases. + * By contrast, the fastCopyFrom() function implements the old, + * more efficient but less safe behavior + * of making this string also a readonly alias to the same buffer. + * + * If the source object has an "open" buffer from getBuffer(minCapacity), + * then the copy is an empty string. + * * @param that The UnicodeString object to copy. * @stable ICU 2.0 + * @see fastCopyFrom */ UnicodeString(const UnicodeString& that);