Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Rainer Weikusat Newsgroups: comp.unix.programmer,comp.lang.c Subject: Re: Odd compiler behaviour? Date: Tue, 08 Mar 2016 12:59:03 +0000 Lines: 66 Message-ID: <871t7ldt2g.fsf@doppelsaurus.mobileactivedefense.com> References: <20160301061135.783@kylheku.com> <87h9gqb0lj.fsf@mantic.terraraq.uk> <878u22aua9.fsf@mantic.terraraq.uk> <87wpplaojy.fsf@mantic.terraraq.uk> <20160305152935.13fa72e4c5e160164248c79c@speakeasy.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: individual.net OYGN+oAn73RmIGJRWorqqwtuspkOhJE/JeS4EhNFwxkeESBfM= Cancel-Lock: sha1:nyINab6tYsooJfOBe5LjpMuCQYA= sha1:WdaMJsHrh4FBNQ8SeHFEgHspibQ= User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Xref: csiph.com comp.unix.programmer:8057 comp.lang.c:83455 spud@potato.field writes: > On Sat, 5 Mar 2016 15:29:35 -0500 > "James K. Lowden" wrote: >>C++ is sometimes criticized for overloaded operators, but IMO the >>criticism should go the other way, because types exist to be operated >>on. Any language that supports user-defined types but not user-defined >>operators is missing an essential component. That's what leads Java >>to wordy constructs like foo.equals(bar) instead of foo == bar. > > The result of adding 2 numbers together, or even strings using + is > obvious. Is it? [rw@duesterwald]~/work $perl -le 'print "2" + "2"' 4 [rw@doppelsaurus]/tmp#cat a.C #include int main(void) { std::cout << "2" + "2" << "\n"; return 0; } [rw@doppelsaurus]/tmp#g++ a.C a.C: In function 'int main()': a.C:5:24: error: invalid operands of types 'const char [2]' and 'const char [2]' to binary 'operator+' [rw@doppelsaurus]/tmp#cat a.java class NotAnObject { public static void main(String[] unused) { System.out.println("2" + "2"); } } [rw@doppelsaurus]/tmp#javac a.java [rw@doppelsaurus]/tmp#java NotAnObject 22 ("1984" example intentional) > Doing myobj1 + myobj2 tells someone else looking at the code later very little > unless they dive into class definitions and it could be argued it simply > obfuscates things. All for the sake of saving a few bytes in the source code > by using an operator instead of calling a function. An operator symbol denotes a principially arbitrary function supposed to be applied to some elements which a members of a certain set. That's mathematics and not C++, hence, C++ is hardly to blame here. [...] > Like exceptions overloading should be used sparingly and only where it > actually aids readability and understanding. Usually however people just use > them because they're of the mindset that the more features of a language they > use the better programmer they are. Followed through to its logical conclusion, it seems clear that only people who don't know how to use any programming language can be considered 'good programmers' :->. Leaving the joke aside, the (supposed) motivation doesn't matter here: Any particular example of "operator overloading" can be considered sensible or not-so-sensible on its own, regardless of who wrote it for which reason.