From: Bruno Haible Date: Sun, 27 Dec 2015 00:03:56 +0000 (+0100) Subject: Add comments about the implementation of 'autosprintf::operator='. X-Git-Tag: v0.20~503 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8735e2d4f1b6fc74ba05059a356f1b2f45775b16;p=thirdparty%2Fgettext.git Add comments about the implementation of 'autosprintf::operator='. * gettext-runtime/libasprintf/autosprintf.in.h (autosprintf::operator=): Add comments. Rename parameter. * gettext-runtime/libasprintf/autosprintf.cc (autosprintf::operator=): Likewise. --- diff --git a/gettext-runtime/libasprintf/autosprintf.cc b/gettext-runtime/libasprintf/autosprintf.cc index 0c8bff1f1..d194a309b 100644 --- a/gettext-runtime/libasprintf/autosprintf.cc +++ b/gettext-runtime/libasprintf/autosprintf.cc @@ -56,10 +56,12 @@ namespace gnu str = (src.str != NULL ? strdup (src.str) : NULL); } - /* Copy constructor. Necessary because the destructor is nontrivial. */ - autosprintf& autosprintf::operator = (autosprintf copy) + /* Assignment operator. Necessary because the destructor is nontrivial. */ + autosprintf& autosprintf::operator = (autosprintf temporary) { - std::swap (copy.str, this->str); + /* Copy-and-swap idiom. + See http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom */ + std::swap (temporary.str, this->str); return *this; } diff --git a/gettext-runtime/libasprintf/autosprintf.in.h b/gettext-runtime/libasprintf/autosprintf.in.h index 1c9eb18a1..9c34bc92b 100644 --- a/gettext-runtime/libasprintf/autosprintf.in.h +++ b/gettext-runtime/libasprintf/autosprintf.in.h @@ -47,7 +47,8 @@ namespace gnu _AUTOSPRINTF_ATTRIBUTE_FORMAT(); /* Copy constructor. */ autosprintf (const autosprintf& src); - autosprintf& operator = (autosprintf copy); + /* Assignment operator. */ + autosprintf& operator = (autosprintf temporary); /* Destructor: frees the temporarily allocated string. */ ~autosprintf (); /* Conversion to string. */