Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #146000
| Subject | Re: in place swap of float types |
|---|---|
| Newsgroups | comp.lang.c |
| References | (6 earlier) <91wbF.219020$893.137679@fx02.am4> <qknvie$as0$1@dont-email.me> <s_MbF.456041$_l5.198989@fx32.am4> <qkofkt$6m7$1@dont-email.me> <ln7e6nwylx.fsf@kst-u.example.com> |
| From | Bart <bc@freeuk.com> |
| Message-ID | <bdWbF.201989$Nw3.199362@fx40.am4> (permalink) |
| Organization | virginmedia.com |
| Date | 2019-09-04 22:42 +0100 |
On 04/09/2019 22:16, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
> [...]
>> In C++, "swap" could not be part of the language because it needs to be
>> modifiable by code. You have to be able to write your own
>> specialisations of swap for your own types, if you want to.
>
> You could make the same argument for sizeof.
>
> There's no fundamental reason C++ couldn't have, say, a swap operator,
> where `a swap b` takes to lvalues of the same (or compatible?) types and
> swaps the contents of the objects they refer to -- or whatever syntax
> you like.
>
> But C's language features (templates, references) are powerful
> enough that a convenient swap() can be defined in the library --
> and in fact it is:
>
> #include <algorithm>
> ...
> std::swap(a, b);
C++ you mean? I think even I can just about manage that; this is the
first template I've ever written (test code elided):
template <class T>
void swap(T &a, T &b) {
T temp;
temp = a;
a = b;
b = temp;
}
(My own static language can't do this, but then 'swap' is built-in. My
dynamic language however could; here I'm defining 'myswap' as 'swap' is
also built-in:
proc myswap(&a,&b)=
swap(a,b) # choice of 3 swap methods
# t:=a; a:=b; b:=t
# (a,b):=(b,a)
end
With the additional ability for a and b to be of completely different
types, and of any complexity, which C++ would have some trouble with.)
> A minor tangent: The common way of writing code to swap two objects is:
> {
> int temp = a;
> a = b;
> b = temp;
> }
> I find this a bit clearer:
> {
> const int old_a = a;
> a = b;
> b = old_a;
> }
> I like defining objects as "const" if they're not going to be
> modified (YMMV), and the name "old_a" is clearer than "temp".
> With the name "temp", it takes me a moment to be sure that the
> assignments are in the right order. Swapping is symmetric, but
> swapping using a temporary introduces a mildly annoying assymetry.
A true swap can be considered symmetric (whether it's done with a left
or right rotation, or uses some other method, is not exposed, so can be
assumed to be either). The way it is commonly written with a temp, uses
left-rotation, so is not symmetric.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
in place swap of float types 3queens@shabbyroad.com - 2019-08-27 17:03 +0000
Re: in place swap of float types Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-08-27 19:22 +0000
Re: in place swap of float types Andrew Smallshaw <andrews@sdf.org> - 2019-09-02 15:25 +0000
Re: in place swap of float types Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-09-02 19:37 +0000
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-03 06:37 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-03 16:13 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-03 16:54 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 11:19 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-04 02:35 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 12:31 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-04 12:12 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 15:53 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-04 07:17 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:12 +0200
Re: in place swap of float types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-09-05 21:11 +0100
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-04 16:30 +0100
Re: in place swap of float types jadill33@gmail.com - 2019-09-04 10:26 -0700
Re: in place swap of float types Ike Naar <ike@iceland.freeshell.org> - 2019-09-05 06:00 +0000
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-05 11:14 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 14:54 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-05 15:48 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 10:35 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-06 02:52 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 13:18 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-06 12:14 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 15:08 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-06 06:23 -0700
Re: in place swap of float types Keith Thompson <kst-u@mib.org> - 2019-09-04 14:16 -0700
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-04 22:42 +0100
Re: in place swap of float types Keith Thompson <kst-u@mib.org> - 2019-09-04 15:21 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:15 +0200
Re: in place swap of float types Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-09-04 08:05 -0600
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:17 +0200
Re: in place swap of float types Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-09-05 20:28 -0600
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 10:39 +0200
Re: in place swap of float types Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-09-06 08:11 -0600
Re: in place swap of float types scott@slp53.sl.home (Scott Lurndal) - 2019-09-06 18:31 +0000
Re: in place swap of float types Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-09-04 19:02 +0000
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-02 22:16 +0200
Re: in place swap of float types Robert Wessel <robertwessel2@yahoo.com> - 2019-08-27 17:59 -0500
Re: in place swap of float types Richard Damon <Richard@Damon-Family.org> - 2019-08-27 17:45 -0700
Re: in place swap of float types James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-27 20:48 -0400
Re: in place swap of float types Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-09-03 06:17 -0700
Re: in place swap of float types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-08-28 02:09 +0100
Re: in place swap of float types Siri Cruise <chine.bleu@yahoo.com> - 2019-08-27 21:26 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-08-28 09:22 +0200
Re: in place swap of float types Andrey Tarasevich <andreytarasevich@hotmail.com> - 2019-08-28 16:36 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-08-28 09:36 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-08-28 12:02 +0100
Re: in place swap of float types James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-28 05:08 -0700
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-08-28 05:26 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-08-28 14:40 +0200
Re: in place swap of float types Keith Thompson <kst-u@mib.org> - 2019-08-28 13:01 -0700
Re: in place swap of float types Philipp Klaus Krause <pkk@spth.de> - 2019-09-04 11:18 +0200
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 12:57 +0200
Re: in place swap of float types Philipp Klaus Krause <pkk@spth.de> - 2019-09-04 13:24 +0200
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 08:18 +0200
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:19 +0200
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 16:18 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-05 07:32 -0700
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 17:32 +0200
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 10:54 +0200
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-06 14:23 +0200
csiph-web