Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: >< swap operator Date: Tue, 02 Oct 2018 14:29:18 -0700 Organization: None to speak of Lines: 50 Message-ID: References: <36d546c5-b471-45fc-b87a-8ebebdc0c0b7@googlegroups.com> <7dcd73d2-4876-4a78-a1b5-940a0cce5120@googlegroups.com> <3YPsD.393167$tw.210351@fx11.am4> <54f174c8-9184-437a-9457-df9be71daf53@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="ab641a093d6af250d7c951df9b0111e8"; logging-data="26932"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ZEvqhojQIZ/qlUXGG0/jb" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:rC3PC7xh1cX+BLTmtMef9m7Y1Sg= sha1:vCXOhvoM09TFJTxGvD0b/eY6O2w= Xref: csiph.com comp.lang.c:136052 scott@slp53.sl.home (Scott Lurndal) writes: > Keith Thompson writes: >>There's no reason the name of an operator has to be made up of >>punctuation characters. Most of them are, but `sizeof` and `_Alignof` >>are both operators, and their names are keywords. >> >>*If* we wanted to add a swap operator to C, it could easily be defined >>as an operator with two operands: >> a swap b >>with whatever semantics seem appropriate. (Of course "swap" is >>already used as an identifier in existing code. We could have a >>_Swap keyword and a new header defining "swap" as a macro.) >> >>I would opppose making "swap" a new built-in operation whose syntax >>makes it look like a function call: >> swap(a, b) >> >>(There's also std::swap in C++, which depends on features that C doesn't >>have.) > > Is there really enough usage of a 'swap' type operation such that > an operator would be genuinely useful? Probably not (thus my emphasis on the "*If*" above). It's a simple enough operation that a predefined operator or (overloaded) function doesn't provide all that much benefit over writing the code inline: int x, y; ... const int old_x = x; x = y; y = old_x; ("temp" is a common name, but I find "old_x" to be more explicit and harder to mess up.) If I'm using a language that provides it as a feature (std::swap in C++, (x, y) = (y, x) in Python, etc), that's great. If not, it's not that big a deal. > I don't recall ever needing that functionality. Now byte swapping > for endianess reasons, that I use daily. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"