Topic: std::swap for noncopyable types
Author: braden@endoframe.com (Braden McDaniel)
Date: Fri, 26 Jul 2002 08:15:06 GMT Raw View
I recently came across the need to swap a noncopyable type I'd
defined. I proceeded to add a swap method to the class, and specialize
std::swap to use the method. It was then pointed out to me that this
application of std::swap seems to be made illicit by the standard
(17.4.3.1):
Such a specialization (complete or partial) of a standard library
template results in undefined behavior ... unless the specialization
meets the standard library requirements for the original template.
The requirement for std::swap is simply that the argument type is
assignable, which obviously would not be the case for my std::swap
specialization for noncopyable type.
I could dispense with my std::swap specialization and just use my swap
method directly; however, the letter of the spec seems unnecessarily
restrictive in this case (and also unlikely to be enforced by a
compiler).
I have read the summary of issue 226 and I did not get the impression
that it covered this specifically.
Braden
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kuyper@wizard.net (James Kuyper)
Date: Sat, 27 Jul 2002 17:47:01 GMT Raw View
braden@endoframe.com (Braden McDaniel) wrote in message news:<ef5cac28.0207241819.6c0343a0@posting.google.com>...
> I recently came across the need to swap a noncopyable type I'd
> defined. I proceeded to add a swap method to the class, and specialize
> std::swap to use the method. It was then pointed out to me that this
> application of std::swap seems to be made illicit by the standard
> (17.4.3.1):
>
> Such a specialization (complete or partial) of a standard library
> template results in undefined behavior ... unless the specialization
> meets the standard library requirements for the original template.
>
> The requirement for std::swap is simply that the argument type is
> assignable, which obviously would not be the case for my std::swap
> specialization for noncopyable type.
Some things are requirements on the implementation of a given standard
library function, and others are requirements on the use of that
function. Requirements on the implementation of a function constrain
the implmentor, while providing the user with useful guarantees.
Requirements on the use of a function constrain the user, while
providing the implementor with useful freedom.
The requirement on the implementation of std::swap(a,b) is that it
must swap a and b. That is the requirement that your specialization
must satisfy. The requirement that the arguments of swap() must have
an assignable type is a requirement on the use of swap(). In general,
it's undefined behavior to violate such requirements, but you're only
using the standard library code when you instantiate the code for
other types. For your own type, you yourself have defined the
behavior, so there should be no problem.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: pdimov@mmltd.net (Peter Dimov)
Date: Sat, 27 Jul 2002 17:47:48 GMT Raw View
braden@endoframe.com (Braden McDaniel) wrote in message news:<ef5cac28.0207241819.6c0343a0@posting.google.com>...
> I recently came across the need to swap a noncopyable type I'd
> defined. I proceeded to add a swap method to the class, and specialize
> std::swap to use the method. It was then pointed out to me that this
> application of std::swap seems to be made illicit by the standard
> (17.4.3.1):
>
> Such a specialization (complete or partial) of a standard library
> template results in undefined behavior ... unless the specialization
> meets the standard library requirements for the original template.
>
> The requirement for std::swap is simply that the argument type is
> assignable, which obviously would not be the case for my std::swap
> specialization for noncopyable type.
Your specialization is legal. Informally speaking, things work as
described under "Effects" when the conditions in "Requires" are met;
otherwise the behavior is undefined (17.4.3.6).
Your class doesn't meet the std::swap requirements, therefore,
application of std::swap to objects of your class is not covered by
the standard. You are free to do anything you want, including meeting
the std::swap effects.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]