Talk:Assignment operator (C++)

message: The example given modifies the state of the object before all operations which might throw exceptions have completed; therefore, the example is not exception-safe.  Also, it uses a built-in array, which makes exception safety difficult in general (though not for ints).  Using std::vector<int> array; instead of int *array; would make exception-safety easier :
class My_Array
{
        int count;
        std::vector<int> array;
public:
        // Copy constructor
        My_Array(const My_Array& orig)
                : count(orig.count), array(orig.array)
        { /* EMPTY */ }
        // Non-throwing swap
        void Swap(My_Array& orig)
        {
                using std::swap;
                swap(count, orig.count);
                swap(array, orig.array);
        }
        My_Array& operator=(const My_Array& rhs)
        {
                My_Array tmp(rhs);
                Swap(tmp);
                return *this;
        }
};
:I have just refactored the code to be exception-safe. However, I've left the built-in array to show the implementation in case of manual resource management.--Tigrisek 22:22, 20 September 2007 (UTC)
thread_title:  Example code – exception safety 

message: I have undid the revision since Koenig Lookup would have the same effect in this case, there's no need to complicate the code.
----
Surely
  std::copy(new_array, new_array + other.count, other.array);
should be 
  std::copy(other.array, other.array + other.count, new_array);
?
The code as-written copies from freshly allocated memory over the original :-(
213.248.204.106 (talk) 09:45, 20 October 2008 (UTC)
replies: text: You're absolutely right, I've just fixed the code.Tigrisek (talk) 20:32, 22 October 2008 (UTC)
thread_title:  Example code – Koenig Lookup 

message: Just as background information.
* http://icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html
* http://icu-project.org/docs/papers/cpp_report/the_assignment_operator_revisited.html  —Preceding unsigned comment added by 213.173.183.22 (talk) 10:18, 27 February 2009 (UTC)
replies: text: In any case the article in question is from 1997, even before the initial C++ Standard, ISO/IEC 14882:1998 was released. As such, there may be more errors or inaccuracies. Regards, decltype (talk) 14:23, 28 September 2011 (UTC)
replies: text: I've added this ref as external reference. AgadaUrbanit (talk) 12:49, 30 December 2011 (UTC)
thread_title:  Add somewhere a Reference to "The Anatomy of the Assignment Operator" 

message: I have reverted the example to the previous form, which is in most regards equal to the new one, but according to the cited reference (that has also been removed with the changes) it is preferred as possibly more optimiser-friendly. Moreover, IIRC, the new code would not compile at all (calling non-const member function on a temporary).Tigrisek (talk) 20:56, 27 July 2009 (UTC)
replies: text: I endorse this. It is idiomatic, and supported by a source. The other example is not ill-formed, however (it is fine to call a non-const member function of a temporary). Regards, decltype (talk) 21:35, 27 July 2009 (UTC)
thread_title:  Undid unjustified changes breaking the code 

message: I'm reverting the edits because they have several issues:
* the order: first acquire new resources, then release the old ones is crucial for exception safety;
** True. Notinlist (talk) 15:20, 8 November 2011 (UTC)
thread_title:  Reverting edits by [[user:Notinlist|Notinlist]] 

message: Could someone provide a quote from  The existing ref does not mention book's pages and this swap approach to assignment appears fishy to me. So meanwhile I'm removing the ref and the cited material (see diff) till clarification is provided. AgadaUrbanit (talk) 12:53, 30 December 2011 (UTC)
replies: text: I've reverted the changes. I'm not going to type the whole quote from the reference (item 56, page 100), but it contains exactly the same code as the article (modulo naming and comments). I see nothing "fishy" in it, it is a known and recommended coding practice.Tigrisek (talk) 16:17, 1 January 2012 (UTC)
thread_title:  Unclear material