Path: csiph.com!news.mixmin.net!news2.arglkargh.de!news.karotte.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Rainer Weikusat Newsgroups: comp.unix.programmer Subject: Re: Odd compiler behaviour? Date: Thu, 10 Mar 2016 19:51:58 +0000 Lines: 41 Message-ID: <87k2lat8kh.fsf@doppelsaurus.mobileactivedefense.com> References: <20160310073739.787@kylheku.com> <20160310141206.690dee00e1890c1c28ec7eed@speakeasy.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: individual.net 90387ITgY/aJ2kiSiq+2kAtfiWbL7fQCxdur+BcC/bRPMbycE= Cancel-Lock: sha1:kciJ7adJ9oUFSfAEVTDZnFYuPvk= sha1:fY5kGsmjQ4QSknQmeFjrYgQC7BA= User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Xref: csiph.com comp.unix.programmer:8125 "James K. Lowden" writes: > spud@potato.field wrote: > >> >It's overloaded; it's doing different kinds of addition. >> >Without overloading, you need a different + operator for >> >floating-point. >> >> Its still addition! The meaning is identical! >> >> The only reason overloading of the maths op is needed is because of >> current CPU architecture. There is no conceptual difference. > > The difference has nothing to do with the CPU and everything to do with > the language. Please see abs(3) and fabs(3). Two functions are needed > because the arguments are of different *types*. That would be true even > on a machine that implemented floating point in software. But the arguments are of different types because of "CPU architectures" supporting different 'kinds' of numbers with different properties and representations. Which is at odd with the naive idea of 'arithmetic' as something which is performed 'on numbers'. This usually includes (sums of) decimal fractions in the common notation for that and usually doesn't include complex numbers or other fractions. > The operator + is a binary function. 'operator +' implementations in C++ are binary functions. But the C '+' operator isn't. It's a special language construct which can appear in an additive expression. If it does, it's supposed to have two operands. Either both must 'have an arithmetic type', ie, be integers or floats, or one must be a pointer and the other an integer. In order to perform an arithmetic addition, the compiler is supposed to determine a 'common real type' for both operands. Both operands are supposed to be converted to this common real type. The addition is then performed with the results of these conversions. Considering that the compiler is supposed to generate code based on the types of the actual operands, the way to approximate this in C++ would really rather be a template function with two template arguments. But that's not good enough because the type of the return value depends on the types of both arguments.