Re: Template-based implementation of Sutter's exception-safe operator= idiom

From: Steven E. Harris (seh_at_panix.com)
Date: 08/21/04


Date: 20 Aug 2004 23:56:00 -0400

David Abrahams <dave@boost-consulting.com> writes:

> Any time you make assignment give the strong guarantee by copying
> and swapping, you force anyone who wants to use assignment in an
> operation which doesn't need that strong guarantee to pay for the
> unneccessary copy, which could be very expensive.

For those who do need the strong guarantee at a higher level, how do
they go about getting it? I understand the "SGI argument" about
concurrency control, but I'm missing the analogous option here to add
exception-safe "locks" at a higher level.

Take your example where a client wants an assignment followed by
push_back() to have the strong guarantee.¹ Is this a potential
solution?

// Neither compiled nor tested.

template <typename C, typename T>
C& safe_assign_push(C& dest, C const& src, T const& val)
{
    C temp( src );
    temp.push_back( val );
    dest.swap( temp );
    return dest;
}

Footnotes:
¹ http://lists.boost.org/MailArchives/boost/msg36928.php

-- 
Steven E. Harris
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


Relevant Pages

  • Re: using swap to make assignment operator exception safe
    ... exception safe (means even if there is exception, ... It used a temporary object "temp" in this sample, ... Not an assignment - initialization, ...
    (microsoft.public.vc.language)
  • RE: Order by effect on temp tables
    ... Informix engine I have used. ... Beware that a SELECT from a table without an ORDER BY clause might not ... Does it mean that if I select values into a temp table using an order ... Would executing the above *guarantee* that the result will be the ...
    (comp.databases.informix)
  • Re: A freshmans question
    ... Do the assignment. ... terrible,I don't understand all of your meaning. ... 0,then don't execute the i++, execute the next statement and print ... temp = i; ...
    (comp.lang.java.programmer)
  • Re: Row Order
    ... to exist in the temp table in the order specified by the ORDER BY clause"? ... Someone told me that there was no guarantee that the rows would get inserted ... SELECT city, state ...
    (microsoft.public.sqlserver.programming)
  • Re: WaitForSingleObject() will not deadlock
    ... Even though the assignment of value 2 is guaranteed ... compiler's guarantee), without the specific guarantee from the thread ... the *compiler* does not guarantee this. ... Any data written to memory after the new thread is ...
    (microsoft.public.vc.mfc)